问题描述
请帮助,我快疯了!
RewriteRule ^([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]
这是我当前的代码.有时人们会访问mysite.com/search,而其他时候他们会访问mysite.com/boris/search,而我检测到用户的空($ _GET ['id'])支票.
This is my current code. Sometimes people will visit mysite.com/search, other times they will visit mysite.com/boris/search and I detect a user with an empty($_GET['id']) check.
但是我正在创建另一个搜索,该搜索指向mysite.com/products/search,该搜索导致了product_search.php
However I am creating another search, mysite.com/products/search which leads to products_search.php
我需要我的原始RewriteRule来匹配除产品"一词外的任何用户.
I need my original RewriteRule to match any user EXCEPT the word 'products'.
我尝试了很多组合.
RewriteRule ^(!products&[a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]
我对regex/mod_rewrite不太满意,但是上面的东西应该可以工作吗?我只需要一个AND运算符就可以清楚地知道&不起作用,但我找不到一个!
I'm not very good with regex/mod_rewrite but I something like the above should work? I just need an AND operator as clearly & doesn't work, but I can't find one!
非常感谢.
推荐答案
您有两个选择:要么使用RewriteCond
来限制已经匹配的字符串:
You have two options: Either use a RewriteCond
to restrict the already matched string:
RewriteCond $1 !=products
RewriteRule ^([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]
或者从Apache 2开始,您还可以使用否定的前瞻断言:
Or since Apache 2, you can also use a negated look-ahead assertion:
RewriteRule ^(?!products/)([a-z0-9_-]+)?/?search/?$ search.php?id=$1&%{QUERY_STRING} [NC,L]
除此之外,您还可以使用 QSA 标志,而不是手动附加 QUERY_STRING .并请注意,您当前的模式还将允许/foobarsearch/
之类的内容,因此它没有分隔的/
.
Besides that, you can also use the QSA flag instead of appending QUERY_STRING manually. And note that your current pattern will also allow something like /foobarsearch/
, so it doesn’t have the separating /
.
这篇关于如何在mod_rewrite中不匹配单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!