需要使用漂亮的URL在查询字符串中传递反斜杠

时间:2015-02-11 00:12:22

标签: php mod-rewrite urlencode pretty-urls

我正在尝试将反斜杠作为查询字符串参数的一部分传递。

该项目使用了一个非常简单的url_rewriting方法:

//.Htaccess
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule .* index.php?arguments=$0 [NC,L]

然后接收$_GET['arguments'],其值由 /

分隔

例如:博客/帖子/ 2 /会将$_GET['arguments']填入:'post / 2 /'

问题是,当我尝试传递类似 test%5Cabc (为 test \ abc 编码的url)时,请求返回404错误而不是传递它作为一个论点

我不知道如何修复它。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您收到404错误的原因是因为默认情况下Apache已关闭AllowEncodedSlashes,其行为是404任何包含反斜杠的请求。这是因为打开它是potential security hazard。在Apache 2.2.18及更高版本中,有一个NoDecode设置可以使它更安全,并且适用于您的情况。

无论如何,我尝试在本地打开它,确实是这个脚本:

<?php var_dump($_REQUEST); ?>

能够处理像

这样的网址
http://localhost/this\is\a\test

和输出

array(1) { ["arguments"]=> string(14) "this\is\a\test" } 

然而,它(或我的浏览器)似乎有编码反斜杠的问题,将它们转换为常规反斜杠 - 可能这不是问题。如果在rewriterule上启用B标志,它会将未编码的反斜杠转换为编码反斜杠。

答案 1 :(得分:0)

我希望我找到你,试着用这个:

'/file.php?id=$value'//this utilise that backslash and then pass the id to the `$_Get[]` Function somewhere in the file.php.