我有这个正则表达式代码:
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文件。 我该如何解决这个问题?
答案 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}
与/?
完全相同。 ?
表示:“零或前一个元素之一。”