.htaccess 301 redirection problem
I am having trouble writing a rule for following problem in .htaccess. Here is what I need.
WHEN URL IS THIS
www.mysite.com/cat/2011/subcat1/subcat2/product.htm?page=2
IT SHOULD 301 REDIRECT TO THIS
www.mysite.c开发者_StackOverflow社区om/cat/2011/subcat1/subcat2/product/2.htm
Can you please tell me how to do this?
Thanks
----- EDITED AND TESTED ANSWER ----------
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(\d+)
RewriteRule ^cat/(\d+)/([^/]+)/([^/]+)/product\.htm http://www.mysite.com/cat/$1/$2/$3/product/%1.htm? [R=301,L]
- RewriteRule matched only the url part, not the query string. So, RewriteCond is used to match query string.
- Please note, matches from the url are used as $N and match from the query string is used as %N
- There is a question mark placed in the end of the rewritten url. This prevents the original query string to be added to the new rewritten url.
For any number of subcats:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(\d+)
RewriteRule ^cat/(\d+)/(.+)/product\.htm http://www.mysite.com/cat/$1/$2/product/%1.htm? [R=301,L]
精彩评论