Combination of rewrites in .htaccess doesn't work

自古美人都是妖i 提交于 2019-12-13 00:45:06

问题


I have different rules in my .htaccess file which work fine individually but combined in one file they don't. Here are some examples of my file:

# take care of %C2%A0
RewriteRule ^(.+)\xc2\xa0(.+)$ $1-$2 [L,NE]

# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]

# executes when there is exactly 1 space in URI 
RewriteRule "^productdetails/617/6/(\S*) (\S*?)/?$" /$1-$2/302 [L,R=302,NE]

Also I've got the following:

RewriteCond %{QUERY_STRING} ^pid=617/?$
RewriteRule ^productdetails\.asp$  /Casio-CDP120-Digital-Piano-in-Black/302? [L,NC,R=301]

which still work fine. I have now added the following:

RewriteRule "^categories/3/Kawai Digital Pianos/?$" /Compare/Kawai-Digital-Pianos [L,NC,R=301]

which used to rewrite:

mysite.co.uk/categories/3/Kawai%20Digital%20Pianos/ to mysite.co.uk/Compare/Kawai-Digital-Pianos

this does not work anymore

Any help to get the last rule working in combination with the others would be great


回答1:


You just need to make sure order of rules is correct. For your examples following order should work:

RewriteRule "^categories/3/Kawai Digital Pianos/?$" /Compare/Kawai-Digital-Pianos [L,NC,R=301]

RewriteCond %{QUERY_STRING} ^pid=617/?$
RewriteRule ^productdetails\.asp$  /Casio-CDP120-Digital-Piano-in-Black/302? [L,NC,R=301]

# take care of %C2%A0
RewriteRule ^(.+)\xc2\xa0(.+)$ $1-$2 [L,NE]

# executes **repeatedly** as long as there are more than 1 spaces in URI
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]

# executes when there is exactly 1 space in URI 
RewriteRule "^productdetails/617/6/(\S*) (\S*?)/?$" /$1-$2/302 [L,R=301,NE]


来源:https://stackoverflow.com/questions/25814592/combination-of-rewrites-in-htaccess-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!