I feel like开发者_JS百科 I'm really close, but I can't quite get this Apache RewriteRule to work correctly.
I have a URL like http://mysite.com/product.php?view=true&ID=123
and I would like to redirect to http://mysite.com/some-page
.But if the product ID=456, then I'd like to redirect it to http://mysite.com/some-other-page
.
So I don't need to keep the query string for my destination URLs, but I do care what it equals in the request because that will determine where I redirect the user to. I've been struggling with trying to understand how to use the mod_rewrite %{QUERY_STRING}
parameter, but I just can't get it to work.
Thanks in advance for your help!
This should help a little bit.
RewriteCond %{QUERY_STIRNG} id=456
RewriteRule .* /some-other-page? [R,L]
More about Manipulating the Query String.
Like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} =view=true&ID=123
RewriteRule ^product\.php$ /some-page? [R=301,L]
RewriteCond %{QUERY_STRING} =view=true&ID=456
RewriteRule ^product\.php$ /some-other-page? [R=301,L]
You need to match
^product.php
as well as query string. I did match FULL EXACT query string (bothview=true
andID=123
need to be present in EXACT order). If you need only partial match (e.g.ID=123
only), then replace=view=true&ID=123
byID=123
.Notice
?
at the end of new URL -- this is to get rid of existing query string. Without it/product.php?view=true&ID=123
will be redirected as/some-page?view=true&ID=123
.I've used 301 Permanent Redirect. You may want to change it to 302 or whatever other redirect code you think is better for you.
This is to be placed in .htaccess in in website root folder. If placed elsewhere some small tweaking may be required.
精彩评论