如何从 htaccess 正则表达式中省略匹配项
RewriteEngine On
RewriteBase /
RewriteRule ^sample-([a-z][a-z]).php /state.php?m=$1
RewriteRule ^sample-([a-z^-]+).php /city.php?m=$1
我的 htaccess 文件中有两个正则表达式,它们查找州(两个字母字符,例如“sample-tx.php”)和城市(字母字符和破折号的任意总和,例如“sample-new-bedford.php”)。 php”)。
这些表达式现在工作得很好,但我需要两件事的帮助:
对于状态表达式,我如何省略“az”和“tx”,所以当有人转到“sample-az.php”或“sample”时-tx.php" 它们没有被重定向?
与#1 相同的问题,只不过这次是针对城市。我如何省略“休斯顿”和“新贝德福德”,这样当有人访问“sample-houston.php”或“sample-new-bedford.php”时,他们就不会被重定向?
RewriteEngine On
RewriteBase /
RewriteRule ^sample-([a-z][a-z]).php /state.php?m=$1
RewriteRule ^sample-([a-z^-]+).php /city.php?m=$1
I have two regular expressions in my htaccess file, which look for a state (two alphabetical characters, for example "sample-tx.php") and city (any sum of alphabetical characters and dashes, for example "sample-new-bedford.php").
These expressions work great right now, but I need help with two things:
For the state expression, how would I omit "az" and "tx", so when someone goes to "sample-az.php" or "sample-tx.php" they are not redirected?
Same question as #1, except this time for cities. How would I omit "houston" and "new bedford", so when someone goes to "sample-houston.php" or "sample-new-bedford.php" they are not redirected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
(?!...)
否定断言为此。将它们写在字符匹配组之前。对于城市来说也是一样的。
You can use
(?!...)
negative assertions for that.Write them immediately before the character matching group. It would work the same for the cities.