apache 中的自动 urldecode mod_rewrite
the auto urldecode in apache mod_rewrite
这是我的 apache .htaccess
设置,带有搜索页面重定向
(RewriteRule ^search/(.*)$ /index.php?c=index&m=search&keywords= [L,QSA]),
如果我在没有 %
的情况下在 url 中设置搜索查询,它会工作 success request without '%' but if a %
char added to the string, apache with return a bad request with % ,但是不是添加 %
而是 %25
, url%
的编码字符串,预编码%
到%25
成功请求
我想知道造成这种情况的原因。我猜想在 .htaccess
的 RewriteRule
中,一个 url 解码函数从 Pattern 到 Substitution,这就是为什么 %
char 不能被服务器识别,但是 %25
能够。如何禁用此 url 解码功能?
- 因为百分号 ("%") 字符用作百分号编码八位组的指示符,它必须被百分号编码为 "%25" 以便该八位组用作 URI 中的数据 (RFC 3986)
- 我认为这是因为 Apache 遵循标准,mod_rewrite 必须在映射之前对 URL 进行转义。(https://httpd.apache.org/docs/current/rewrite/flags.html#flag_b)
"disable" 自动 url 解码的简单解决方法是在 RewriteCond 指令 (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,否则你会得到解码后的 url。
如果你想让所有的 $_SERVER 做你需要的,请使用 THE_REQUEST 代替:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/search/([^\s]*)
RewriteRule . test.php?c=index&m=search&keywords=%1 [L,QSA]
我们只需要将 . (点)在 RewriteRule 模式中并使用 %1 而不是 $1
这是我的 apache .htaccess
设置,带有搜索页面重定向
(RewriteRule ^search/(.*)$ /index.php?c=index&m=search&keywords= [L,QSA]),
如果我在没有 %
的情况下在 url 中设置搜索查询,它会工作 success request without '%' but if a %
char added to the string, apache with return a bad request with % ,但是不是添加 %
而是 %25
, url%
的编码字符串,预编码%
到%25
我想知道造成这种情况的原因。我猜想在 .htaccess
的 RewriteRule
中,一个 url 解码函数从 Pattern 到 Substitution,这就是为什么 %
char 不能被服务器识别,但是 %25
能够。如何禁用此 url 解码功能?
- 因为百分号 ("%") 字符用作百分号编码八位组的指示符,它必须被百分号编码为 "%25" 以便该八位组用作 URI 中的数据 (RFC 3986)
- 我认为这是因为 Apache 遵循标准,mod_rewrite 必须在映射之前对 URL 进行转义。(https://httpd.apache.org/docs/current/rewrite/flags.html#flag_b)
"disable" 自动 url 解码的简单解决方法是在 RewriteCond 指令 (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,否则你会得到解码后的 url。 如果你想让所有的 $_SERVER 做你需要的,请使用 THE_REQUEST 代替:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/search/([^\s]*)
RewriteRule . test.php?c=index&m=search&keywords=%1 [L,QSA]
我们只需要将 . (点)在 RewriteRule 模式中并使用 %1 而不是 $1