Apache mod_rewrite中的自动urldecode [英] the auto urldecode in apache mod_rewrite

查看:105
本文介绍了Apache mod_rewrite中的自动urldecode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Apache .htaccess 设置,带有搜索页面重定向

this is my apache .htaccess setting with a search page redirect

( RewriteRule ^ search /(.*)$ /index.php?c=index&m=search&keywords=$1 [L,QSA]),

如果我在没有的url中设置搜索查询,则它可以没有'%'的成功请求,但是如果字符添加到字符串中,则apache返回有%的错误请求,但是不要添加%25 的urlencoded字符串,预编码%时的成功请求%25

if I set the search query in url without %, it works success request without '%' but if a % char added to the string, apache with return a bad request with % , howerver instead of adding % but %25, the urlencoded string of %, success request when pre encode % to %25

我想知道这种情况的原因。我猜想在 .htaccess RewriteRule 中,一个urldecode函数从Pattern到Substitution工作,这就是为什么%字符,但%25 可以。如何禁用此urldecode功能?

I want to know about the reason of this situation. I guess in the RewriteRule of .htaccess an urldecode function worked from Pattern to Substitution, that's why % char cannot be recognized by the server, but %25 can. How can I disable this urldecode function?

推荐答案


  • 因为百分比(%)字符用作百分比编码八位字节的指示符,该字节必须被百分比编码为%25才能用作URI(RFC 3986)中的数据。

  • 我认为这是因为Apache遵循了标准,mod_rewrite必须进行映射之前,请先对它们进行unescape。。(( https://httpd.apache。 org / docs / current / rewrite / flags.html#flag_b

    • Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI (RFC 3986)
    • I think it's because Apache follows the standards, mod_rewrite has to unescape URLs before mapping them.. (https://httpd.apache.org/docs/current/rewrite/flags.html#flag_b)
    • 轻松禁用自动 URL解码的解决方法是在RewriteCond指令中使用服务器变量(如 REQUEST_URI THE_REQUEST http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteCond

      Easy workaround for "disable" auto url decoding is to use Server-Variables like REQUEST_URI or THE_REQUEST in RewriteCond Directive (http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteCond)

      RewriteEngine on
      RewriteCond %{REQUEST_URI} /search/(.*)
      RewriteRule . test.php?c=index&m=search&keywords=%1 [L,QSA]
      

      如果使用REQUEST_URI,则在PHP中必须使用$ _SERVER ['REQUEST_URI']。请勿使用$ _SERVER ['QUERY_STRING']或$ _GET,否则您将获得解码后的网址。
      如果要让所有$ _SERVER进行所需的操作,请改用THE_REQUEST:

      If you use REQUEST_URI, in PHP you must use $_SERVER['REQUEST_URI']. Do not use $_SERVER['QUERY_STRING'] or $_GET, otherwise you will get the decoded url. If you want all $_SERVER doing what you need, use THE_REQUEST instead:

      RewriteEngine on
      RewriteCond %{THE_REQUEST} \s/search/([^\s]*)
      RewriteRule . test.php?c=index&m=search&keywords=%1 [L,QSA]
      

      而且我们只需要放。 (点)在RewriteRule模式中,并使用%1代替$ 1

      And we just need to put . (dot) in RewriteRule pattern and use %1 instead of $1

      这篇关于Apache mod_rewrite中的自动urldecode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆