重写规则不工作与加号(+或*)字符 [英] RewriteRule is not working with plus (+ or *) character
本文介绍了重写规则不工作与加号(+或*)字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
RewriteRule ^([a-z]).php$ /index.php?zig=$1 [NC,L] # working
该规则是否正常工作。但
This rule is working correctly. But
RewriteRule ^([a-z]+).php$ /index.php?zig=$1 [NC,L] # not working
或
RewriteRule ^([a-z]\+).php$ /index.php?zig=$1 [NC,L] # not working
不工作。不同的是( +
)。如何使用 +
在code以上?
Is not working. Difference is (+
). How to use +
in the code above?
推荐答案
这条规则是好的:
RewriteRule ^([a-z]+)\.php$ /index.php?zig=$1 [NC,L]
但会产生因为改写URI的无限循环的index.php
也匹配的正则表达式。为prevent这需要夫妻从此改写像preventing的文件/目录的变化和逃生点,因为它是一种特殊的正则表达式元字符:
but will create an infinite loop since rewritten URI /index.php
also matches the regex pattern. To prevent this you need couple of changes like preventing files/directories from this rewrite and escape the dot as it is a special regex meta character:
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)\.php$ index.php?zig=$1 [QSA,NC,L]
QSA
(查询字符串追加)标志preserves现有的查询参数,同时增加了新的。
QSA
(Query String Append) flag preserves existing query parameters while adding a new one.
这篇关于重写规则不工作与加号(+或*)字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文