【问题标题】:Need to pass backslashes in query string with pretty urls需要在带有漂亮 url 的查询字符串中传递反斜杠
【发布时间】:2015-02-11 00:12:22
【问题描述】:

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

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

//.Htaccess
RewriteEngine On

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

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

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

分隔

例如: blog/post/2/ 将填写 $_GET['arguments']'post/2/'

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

我不知道如何解决它。提前致谢。

【问题讨论】:

    标签: php mod-rewrite urlencode pretty-urls


    【解决方案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" } 
    

    但是,它(或我的浏览器)似乎存在编码反斜杠的问题,将它们转换为常规反斜杠 - 也许这不是问题。如果您在重写器上启用 B 标志,它会将未编码的反斜杠转换为编码的反斜杠。

    【讨论】:

      【解决方案2】:

      希望我没听错,试试这个:

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

      【讨论】:

      • 感谢您的回答。我已经让它工作了,问题是当我尝试传递这样一个字符串时:'test\\abc'。已经在使用 urlencode。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2023-04-04
      • 2015-05-18
      • 2011-12-15
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      相关资源
      最近更新 更多