php 使用 mod_rewrite 将带有哈希字符的路径转换为查询字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4960513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:22:13  来源:igfitidea点击:

Using mod_rewrite to convert paths with hash characters into query strings

phpapachemod-rewritehashurlencode

提问by Mark

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

我有一个 PHP 项目,我需要在 URL 的路径中发送一个哈希字符 (#)。(http://www.example.com/parameter#23/parameter#67/index.php)我认为 urlencode 会允许,将哈希转换为 %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).

但现在我看到,即使是 urlencoded 哈希也会强制浏览器将右侧的所有内容都视为 URL 片段(或查询)。

Is there a way to pass a hash through, or do I need to do a character substitution prior to urlencode?

有没有办法传递散列,或者我是否需要在 urlencode 之前进行字符替换?

Edit to add (Sep 19 2017):

编辑添加(2017 年 9 月 19 日):

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.

事实证明我问错了问题。我的问题不是在路径中使用哈希字符(编码它确实有效),而是使用 mod_rewrite 将其转换为查询字符串。我未能在 RewriteRule 中重新编码它。我将编辑标题以匹配。

Here is the rewrite rule I was using:

这是我使用的重写规则:

RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php"      /hashtags.php?parameter_1=&parameter_2= [QSA,L]

As soon as I added the B tag, it worked correctly:

一旦我添加了 B 标签,它就可以正常工作:

RewriteEngine On
# convert path strings into query strings
RewriteRule "^(.*)/(.*)/hashtags.php"      /hashtags.php?parameter_1=&parameter_2= [QSA,L,B]

回答by sparkyspider

Encode the Hash in the URL with %23

使用 %23 对 URL 中的哈希进行编码

http://twitter.com/home?status=I+believe+in+%23love

"I believe in #love"

“我相信爱情”

URL Encoding Reference: http://www.w3schools.com/tags/ref_urlencode.asp

URL编码参考:http: //www.w3schools.com/tags/ref_urlencode.asp