How to do a htaccess rewrite with a question mark ? in the original url
I've got to redirect a URL that has a question mark in it, and I just can't开发者_运维百科 figure out the proper .htaccess code to make it happen.
Example: redirect 301 /home.php?cat=16 http://www.example.com/furniture.html
I believe I need to do a rewrite condition, but I'm no htaccess expert.
With the Redirect
directive from mod_alias you can only examine the URI path and not the query (applies to all directives of mod_alias). If you want to examine the URI query you need to use mod_rewrite:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=16$
RewriteRule ^home\.php$ http://www.example.com/furniture.html? [L,R=301]
The empty query in the replacement will prevent that the original query is being appended to the new URI.
From the apache2 mod_rewrite
docs:
What is matched?
The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string. If you wish to match against the hostname, port, or query string, use a
RewriteCond
with the%{HTTP_HOST}
,%{SERVER_PORT}
, or%{QUERY_STRING}
variables respectively.
for more complex parameters (like site.com/index.php?blaa=1&id=33 )
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blaa=1&id=33
RewriteRule ^(.*)$ http://www.site.com/yourpage.html? [R=301,L]
</IfModule>
精彩评论