我应该在 htaccess RewriteRule 中使用什么正则表达式来匹配 URI 中的 %?
当用户在浏览器中输入以下内容时: http://cauzoom.com/collection/something0%d 我希望他们最终到达这里: http://cauzoom.com/collection/index.html?pname=something
我当前的 htaccess 重写规则如下所示:
RewriteRule ^collection/([a-z0-9\-\%]+)$ collection/index.html?pname=$1 [NC,L]
...但是失败了。当我这样做时:
RewriteRule ^collection/(.+)$ collection/index.html?pname=$1 [NC,L]
...它也失败了。我知道 % 是受保护的正则表达式字符,但是将文字“\”放在它前面应该可以工作,对吧?有什么建议吗?
When a user types this into a browser:
http://cauzoom.com/collection/something0%d
I want them to end up here:
http://cauzoom.com/collection/index.html?pname=something
My current htaccess rewrite rule looks like this:
RewriteRule ^collection/([a-z0-9\-\%]+)$ collection/index.html?pname=$1 [NC,L]
... but that fails. When I do this:
RewriteRule ^collection/(.+)$ collection/index.html?pname=$1 [NC,L]
... it also fails. I know % is a protected regexp character, but putting the literal "\" in front of it SHOULD work, right? Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
%
不是正则表达式元字符,问题是它用于对 URL 中的字符进行编码,因此您不能只在 URL 中使用文字%
意味着开始一个识别特殊字符的序列:参考如果你想有一个真正的
%
在您必须将 URL 编码为%25
。%
isn't a regular expression metacharacter, the problem is that it's used to encode characters in URLs, so you can't just use a literal%
in an URL as it's meant to start a sequence which identifies a special character: referenceIf you want to have a real
%
in an URL you have to encode it as%25
.