我正在创建一个重写规则,其中应包含一个可能包含字符 %
的参数,但是当我将其添加到我的规则中时,它会破坏我的网站,并且每个页面都会返回一个错误:
RewriteRule ^sale/([a-zA-Z0-9_-%]+)$ browse.php?id=$1
我希望该参数能够包含字符、数字 0 到 9 以及特殊字符 -
、_
和 %
。
如果我删除 %
它工作正常,但显然我希望它被接受为字符,例如 url :
http://www.websitename.com/sale/test%20parameter
I'm creating a rewrite rule which should include a parameter which could contain the character %
, however when I add it to my rule it breaks my website and every page returns an error:
RewriteRule ^sale/([a-zA-Z0-9_-%]+)$ browse.php?id=$1
I wanted the parameter to be able to include characters, digits 0 to 9 and special characters -
, _
and %
.
If I remove the %
it works fine but obviously I want that to be accepted as a character for example url :
http://www.websitename.com/sale/test%20parameter
发布评论
评论(2)
Apache 在将 URL 中的百分比编码字符提供给 mod_rewrite 之前对其进行翻译。因此,如果您想在网址中接受
%20
,您只需在 RewriteRule 中添加一个空格即可。但请注意,空格也用于分隔 RewriteRule 中的正则表达式和替换字符串,因此在使用空格的情况下,您需要使用\
对其进行转义Apache translates percentage-encoded character inside a url before feeding it to mod_rewrite. So if you want to accept
%20
in your urls you need to just add a space inside your RewriteRule. Note however that space is also to separate the regulare expression and the replacement string in RewriteRule, so in the case of a space you need to escape it using\
您可以使用
B 标志
http:// httpd.apache.org/docs/2.2/rewrite/flags.html#flag_bYou can use the
B flag
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b