需要在 .htaccess 重写规则中转义 #(哈希/井号)字符
这个问题相当简单,但我几个小时都找不到答案。
我需要做的是:
RewriteRule ([^#])#(.*) $1\%23$2
这基本上意味着我想 url 转义从外部代码片段中得到的奇怪的哈希符号。
反斜杠 (\
) 无法转义此符号...并且请不要建议使用 %23
代替 #
,因为它不会工作也一样。
(%2
3 与 #
不匹配,因为它根本不是 == %23
)
The question is fairly simple but I was not able to find an answer for hours now.
What I need to do is:
RewriteRule ([^#])#(.*) $1\%23$2
Which basically means I want to url escape the freaking hash sign which comes to me from an external codepiece.
backslash (\
) does not work to escape this sign... and please don't suggest using %23
instead #
because it does not work as well.
(%2
3 does not match a #
because it simply is not == %23
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚在这个论坛上发布了几篇帖子后,在一个网站上实现了此功能,我使用的是 NE 不转义和 R=301 重定向选项的重写规则:
这会将所有画廊/变量重定向到< em>/gallery.html#/variable
编辑:规则的重要部分是 NE,它指示服务器解析输出而不转义字符。如果没有这个,它将尝试转义重写规则中的#,这正是OP所询问的。
I just got this working for a site following a couple of posts on this forum, I'm using a rewrite rule with NE not escape and R=301 redirect options:
This redirects all galleries/variable to /gallery.html#/variable
Edit: The important part of the rule is NE which instructs the server to parse output without escaping characters. Without this, it will try and escape the # in the rewrite rule which is what the OP is asking about.
URL 的哈希部分不可重写。当 Web 浏览器向 Web 服务器发送 URL 请求时,它会将所有内容发送到哈希符号。哈希值仅在客户端可用(例如 JavaScript 代码可以看到它)。
The hash part of a URL is not available for rewriting. When a web browser sends a URL request to a web server it sends everything up to the hash sign. The hash is only available on the client (e.g. JavaScript code can see it).
在 http 中搜索扩展重定向 ://httpd.apache.org/docs/2.0/misc/rewriteguide.html。对于你的问题有一个很好的解决方案。
Search Extended Redirection in http://httpd.apache.org/docs/2.0/misc/rewriteguide.html . There is a nice solution for your question.
.htaccess
重定向.php
.htaccess
redirect.php