htaccess 重写规则在参数中接受 % 字符

发布于 2025-01-07 09:51:09 字数 456 浏览 2 评论 0 原文

我正在创建一个重写规则,其中应包含一个可能包含字符 % 的参数,但是当我将其添加到我的规则中时,它会破坏我的网站,并且每个页面都会返回一个错误:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2025-01-14 09:51:09

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 \

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文