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

标签 php apache mod-rewrite hash urlencode

我有一个 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]

最佳答案

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

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

“我相信#love”

URL 编码引用:http://www.w3schools.com/tags/ref_urlencode.asp

关于php - 使用 mod_rewrite 将带有哈希字符的路径转换为查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960513/

相关文章:

php - htaccess 没有重写我的 GET 请求的 url

php - 实现 RewriteRule 的更好方法?

regex - 在 mod_rewrite 规则中转换为小写

javascript - 如何在ajax调用中返回成功

php - MD5 密码的 SQL 更新

apache - 使用浏览器语言和非 www 域进行 mod_rewrite

django - 使用 mod_wsgi 在 Apache2 上部署 Django - Django 项目的正确位置?

php - 使用 php 和 MWS 检索库存

php - Amazon SES 中的特殊字符

php - 如何在 MAMP PRO v3.4 中更新 MySQL 版本