开发者

How to create RewriteRule where the request URL has query string

开发者 https://www.devze.com 2023-04-01 11:58 出处:网络
I feel like开发者_JS百科 I\'m really close, but I can\'t quite get this Apache RewriteRule to work correctly.

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]
  1. You need to match ^product.php as well as query string. I did match FULL EXACT query string (both view=true and ID=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 by ID=123.

  2. 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.

  3. 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.

  4. This is to be placed in .htaccess in in website root folder. If placed elsewhere some small tweaking may be required.

0

精彩评论

暂无评论...
验证码 换一张
取 消