1

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

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 正则表达式将我重定向到 products.php 文件。我怎样才能解决这个问题?

4

2 回答 2

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]
于 2013-01-07T15:20:40.933 回答
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}与 完全相同/?。意思是: “?零或前面的元素之一。”

于 2013-01-07T15:26:07.107 回答