正则表达冲突

时间:2013-01-07 15:18:56

标签: regex mod-rewrite redirect

我有这个正则表达式代码:

RewriteCond  %{REQUEST_URI} !/login([/]{0,1}) [NC]
RewriteCond  %{REQUEST_URI} !/search/([/]{0,1}) [NC]
RewriteCond  %{REQUEST_URI} !/search/([^.]+)([/]{0,1}) [NC]

RewriteRule ^([^.]+)/?$ products.php/$1/ [NC,L]
RewriteRule ^([^.]+)/([^.]+)/?$ products.php/$1/$2/ [NC,L]
RewriteRule ^login([/]{0,1})?$ login.php [NC,L]
RewriteRule ^search([/]{0,1})?$ search.php [NC,L]
RewriteRule ^search/([^.]+)/?$ search.php/$1/ [NC,L]

问题是当我去site / search / asdf regex将我重定向到products.php文件。 我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

你能不能只改变RewriteRule的顺序,从最不具体到最不具体:

RewriteRule ^search/([^.]+)/?$ search.php/$1/ [NC,L]
RewriteRule ^search([/]{0,1})?$ search.php [NC,L]
RewriteRule ^login([/]{0,1})?$ login.php [NC,L]
RewriteRule ^([^.]+)/([^.]+)/?$ products.php/$1/$2/ [NC,L]
RewriteRule ^([^.]+)/?$ products.php/$1/ [NC,L]

答案 1 :(得分:1)

除了诉诸你的正则表达式之外,不需要{0,1}

RewriteRule ^search/([^.]+)/?$ search.php/$1/ [NC,L]
RewriteRule ^search(/?)?$ search.php [NC,L]
RewriteRule ^login(/?)?$ login.php [NC,L]
RewriteRule ^([^.]+)/([^.]+)/?$ products.php/$1/$2/ [NC,L]
RewriteRule ^([^.]+)/?$ products.php/$1/ [NC,L]

[/]{0,1}/?完全相同。 ?表示:“零或前一个元素之一。”