htaccess rewrite with 4 query string - apache

I'm trying to rewrite my URLs but i'm so rusty with htaccess.
The url I must to rewrite is something like
http://api.example.com/endpoint.php?publicKey=VALUE1&secretKey=VALUE2&format=VALUE3&callback=VALUE4
Must to rewrite in
http://api.example.com/VALUE1/VALUE2/VALUE3/VALUE4
So here's what I've done with my htaccessfile:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /endpoint.php?publicKey=$1&secretKey=$2&format=$3&callback=$4 [L]
This isn't working as expected cause I can't read one of these variables in PHP. Even that, I can't reach the page if I don't put endpoint.php before the parameters eg: http://api.example.com/endpoint.php/... that's not the
needed behavior. How I can rewrite the URLs as expected?

Related

Redirecting all urls, including no path, to a file in subdirectory

I have checked a large amount of existing answers regarding .htaccess redirects. However none of them have helped me.
What I want to accomplish is redirecting all request urls to /api/init.php. However I've only gotten so far to where my index page www.example.com simply gives me a file listing because of the missing index.php file, while every url request with a path is working.
How can I accomplish this with .htaccess without ending up with a directory listing on my landing page?
This is as far as I got:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /api/init.php?path=$1 [NC,L,QSA]
Well your site root is a directory, so this rule you have excludes existing directories. What you could do is only exclude existing files, and allow existing directories to be handled by the PHP script. Like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /api/init.php?path=$1 [L,QSA]
I removed the NC flag as it's not needed. I added a condition to prevent an unnecessary file-system check.
You don't have to pass the path on in a URL parameter, as you could get it from $_SERVER['REQUEST_URI'] in PHP (not the same as REQUEST_URI in mod_rewrite, in PHP it always has the original URI). If you wanted to do that then your rule becomes nice and simple:
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/api/init.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /api/init.php [L]
Because the query string will just be passed on unaffected (so QSA is not needed).

URL rewriting by htaccess

I would like to rewrite URLs using htaccess like, in the example below.
My HTTP request is:
http://example.com/store1/index.php?page=user/test
I would like to get the URL in browser as:
http://example.com/store1/user/test.**
Also, please note, that the 1st parameter followed by the slashes after example.com is not a real folder; so I am already rewriting it, using this htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+[^/](.*)$ /$1 [QSA]
and it works.
But I need to achive the˛htaccess rewriting, without affecting the above settings.

htaccess rewrite .html not required / is optional

I have a working website, with atleast 500 pages ranked in Google.
All pages have .html at end of page.
Now I want to remove .html of all pages, but let the pages in Google (with .html) keep there index.
After searching I cant find the correct answer.
I know the ? is for optional. I tried 2 Rules behind eachother but didnt work too.
Here is what my htaccess now is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
I tried with adding:
RewriteRule ^(.*)$ find_page.php?redirect=$1&%{QUERY_STRING}
So if URL contains no extension use this rule, else use the normal rule (with htaccess)
I should expect my rule should be something like this: ^(.*)(?\.html)$
So my goal is: With or without html should work, but .php shouldnt be work :-)
Why look for a complex solution?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.html$ find_page.php?redirect=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)?$ find_page.php?redirect=$1 [L,QSA]
This rewrites all request to that php script, adding the original "file name" as parameter "redirect" and preserves all query parameters. That is what you asked for in your question.
But a warning: you can do this and it will allow to rewrite requests to for example page "redirection" as .../redirection?somearg or .../redirection.html?somearg. But for google both request are completely different pages. This will not help you to preserve any ratings when shifting to the new request scheme.
And a general side note: if you have control over the http server configuration, then you should always prefer to place such rules in the hosts configuration instead of using .htaccess style files. Such files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used in two cases: if you do not have control over the http server configuration or if you require your scripts to do dynamic changes to your ruleset (which is always a very insecure thing).
Ok solved my problem.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.html find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ find_page.php?redirect=$1&%{QUERY_STRING} [L,QSA]
With this option there will be checked if the page has .html optional at end. If it has, will the first rule be matched, else will go further and use the second rule which has no html at the end
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html
You don't need find_page.php for redirection. As it mentioned in other answer http://server/folder/file and http://server/folder/file.html becomes the same for the user but different for the Google.
This does not affect to PHP, folders and other content. It just tries to add «.html» to requested URL if it does not point a file or folder.
I've checked, it works fine even user queries uri with anchor like 1.html#bookmark1

URL rewrite rule that allows long URL to appear shorter

I would like to users to visit:
domain.com/example
and have the content from the following page displayed:
domain.com/directory1/directory2/directory3/example.html
In the browser, I would like it to say domain.com/example without redirecting. We have a series of landing pages that need to have short URLs within the domain.
I considered doing something programmatically such as a PHP include however I felt an .htaccess rewrite rule would be best practice.
So you want:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^example$ /directory1/directory2/directory3/example.html [L]
If you want it to work for every file (replacing "example" with any file) then replace the RewriteRule line with:
RewriteRule ^([^/]+)$ /directory1/directory2/directory3/$1.html [L]

.htaccess rewrite query string as path

I've searched for this question but I only come across really specific answers that seem difficult to tailor to my specific needs.
Let's say the URL I'm attempting to rewrite is this:
http://www.example.org/test.php?whatever=something
I want to rewrite it so that it appears as this:
http://www.example.org/test/something
How can I do this?
In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$1 [L]
And in order to redirect the query string URL to the nicer looking one:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php\?whatever=([^\&\ ]+)
RewriteRule ^/?test\.php$ /test/%1? [L,R=301]