apache - 如何在 Apache 中使用 mod_rewrite 对特殊字符进行编码?

标签 apache mod-rewrite escaping

我希望为我的标记系统提供漂亮的网址以及所有特殊字符:+&#%=。有没有办法用 mod_rewrite 来做到这一点,而不必对链接进行双重编码?

我注意到,delicious.com 和 stackoverflow 似乎能够处理单编码的特殊字符。神奇的公式是什么?

这是我想要发生的事情的示例:

http://www.example.com/tag/c%2b%2b

将触发以下 RewriteRule:

RewriteRule ^tag/(.*)   script.php?tag=$1

标签的值为“c++”

apache/mod_rewrite 的正常操作不是这样工作的,因为它似乎将加号变成了空格。 如果我将加号双重编码为​​“%252B”,那么我会得到所需的结果 - 但它会导致 URL 困惑,对我来说似乎很 hack。

最佳答案

The normal operation of apache/mod_rewrite doesn't work like this, as it seems to turn the plus signs into spaces.

我认为实际情况并非如此。 Apache 正在将路径部分中的 %2B 解码为 + ,因为 + 是其中的有效字符。它在让 mod_rewrite 查看请求之前执行此操作。

然后 mod_rewrite 将您的请求“/tag/c++”更改为“script.php?tag=c++”。但在 application/x-www-form-encoded 格式的查询字符串组件中,转义规则与路径部分中应用的转义规则略有不同。特别是,“+”是空格的简写(也可以编码为“%20”,但这是我们现在永远无法更改的旧行为)。

因此 PHP 的表单读取代码接收“c++”并将其作为 C-space-space 转储到您的 _GET 中。

看起来解决这个问题的方法是使用重写标志“B”。请参阅http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteflags - 奇怪的是它使用了或多或少相同的示例!

RewriteRule ^tag/(.*)$ /script.php?tag=$1 [B]

关于apache - 如何在 Apache 中使用 mod_rewrite 对特殊字符进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/459667/

相关文章:

php - httpd libphp5.so ldap错误

apache - 在特定目录上使用 mod_evasive

apache - 将Apache升级到2.4后,内部URL重写不再起作用

string - 使用需要转义的字符在 golang 中创建一个字符串

Java转义Html特殊字符

apache - Tomcat Apache 服务器重启要求过于频繁

php脚本执行python脚本写入一些文件

apache - 使用 InheritDownBefore 看起来与 RewriteOptions 中的 InheritBefore 相同

php - htaccess 中的点给出 500 内部错误

bash - 在 sed 替换字符串中转义换行符