使用 mod_rewrite 将带有哈希字符的路径转换为查询字符串
我有一个 PHP 项目,需要在 URL 路径中发送哈希字符 (#)。 (http://www.example.com/parameter#23/parameter #67/index.php)我认为 urlencode 会允许这样做,将哈希值转换为 %23
但现在我发现,即使是 urlencoded 哈希值也会迫使浏览器将右侧的所有内容视为 URL 片段(或查询) )。
有没有办法传递哈希值,或者我是否需要在 urlencode 之前进行字符替换?
编辑添加(2017年9月19日):
事实证明我问错了问题。我的问题不在于在路径中使用哈希字符(对其进行编码确实有效),而是使用 mod_rewrite 将其转换为查询字符串。我未能在 RewriteRule 中重新编码它。我将编辑标题以匹配。
这是我使用的重写规则:
RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php" /hashtags.php?parameter_1=$1&parameter_2=$2 [QSA,L]
添加 B 标签后,它就正常工作了:
RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php" /hashtags.php?parameter_1=$1&parameter_2=$2 [QSA,L,B]
I have a PHP project where I need to send a hash character (#) within the path of a URL. (http://www.example.com/parameter#23/parameter#67/index.php) I thought that urlencode would allow that, converting the hash to %23
But now I see that even the urlencoded hash forces the browser to treat everything to the right as the URL fragment (or query).
Is there a way to pass a hash through, or do I need to do a character substitution prior to urlencode?
Edit to add (Sep 19 2017):
It turns out that I was asking the wrong question. My issue wasn't with using the hash character within the path (encoding it does work), but in using mod_rewrite to convert it over to a query string. I had failed to re-encode it within the RewriteRule. I'll edit the title to match.
Here is the rewrite rule I was using:
RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php" /hashtags.php?parameter_1=$1¶meter_2=$2 [QSA,L]
As soon as I added the B tag, it worked correctly:
RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php" /hashtags.php?parameter_1=$1¶meter_2=$2 [QSA,L,B]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 %23 对 URL 中的哈希值进行编码
“我相信 #love”
URL 编码参考:http://www.w3schools。 com/tags/ref_urlencode.asp
Encode the Hash in the URL with %23
"I believe in #love"
URL Encoding Reference: http://www.w3schools.com/tags/ref_urlencode.asp