.htaccess中重写的组合不起作用

时间:2014-09-12 18:29:59

标签: apache .htaccess mod-rewrite

我的.htaccess文件中有不同的规则,它们可以单独运行,但组合在一个文件中它们不是。 以下是我的文件的一些示例:

# 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]

此外,我还有以下内容:

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

仍然可以正常工作。 我现在添加了以下内容:

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

用于重写:

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

这不再适用

让最后一条规则与其他规则结合使用的任何帮助都会很棒

1 个答案:

答案 0 :(得分:0)

您只需要确保规则的顺序是正确的。对于您的示例,以下顺序应该有效:

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]