I have a URL that is in the format http://www.example.com/?s=query
I want to redirect this URL to http://www.example.com/search/query
I have the following .htaccess
but I wanted to check if there is anything wrong with this. My RewriteRule
looks a little wonky and I don't know if it will cause problems for other URLs.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^s=(.*)$ [NC]
RewriteRule ^$ /search/%1? [NC,L,R]
I ran a test Here and it seems to redirect to the correct URL.
CodePudding user response:
RewriteCond %{QUERY_STRING} ^s=(.*)$ [NC] RewriteRule ^$ /search/%1? [NC,L,R]
You will likely need the NE
(noescape
) flag on the RewriteRule
directive if you are receiving a %-encoded URL parameter value, otherwise the target URL will be doubly-encoded. The QUERY_STRING
server variable is not decoded by Apache.
It also depends on how you are rewriting /search/query
back to /?s=query
(or presumably more like /index.php?s=query
?) - presumably you are already doing this later in the config? You only want this redirect to apply to direct requests and not rewritten requests (otherwise you'll get a redirect loop). An easy way to ensure this is to check that the REDIRECT_STATUS
env var is empty.
For example:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^s=(.*) [NC]
RewriteRule ^$ /search/%1 [NE,QSD,R,L]
Other points:
- The
QSD
flag would be preferable (on Apache 2.4) to appending?
to the end of the susbtitution string in order to remove the query string. - The regex
^s=(.*)
(the trailing$
was superfluous) does assume thats
is the only URL parameter at the start of the query string. As it stands, everything is assumed to be part of this value. eg.s=foo&bar=1
will result in/search/foo&bar=1
. - The
NC
flag on theRewriteRule
directive is superfluous. - Should you also be checking for
/index.php?s=<query>
? (Or whatever file/DirectoryIndex is handling the request.)