htaccess 规则将 %3F 重写为 ? 并将 %3D 重写为 =

htaccess 规则将 %3F 重写为 ? 并将 %3D 重写为 =

我需要将包含 %3F 的网站 URL 重写为 ? 并将 %3D 重写为 =

我试过这个规则:

  RewriteCond %{HTTP_HOST} myhostname    
  RewriteRule ^(.*)%3F(.+)%3D(.+)$ $1?$2=$3 [L,R=301]

和这个:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\%]+)\%3[Ff]\%3[dD]([^\ ]+)\ HTTP/
RewriteRule \.*$ %1?%2=$3 [R=301,L]

但都不起作用。

请帮忙。谢谢。

答案1

刚刚找到解决方案。添加以下规则即可:

# If THE_REQUEST contains a URL-path with a percent-encoded "?" and/or a query string with one
# or more specific percent-encoded characters, and we're not already in the process of fixing
# it, then copy the client-requested URL-path-plus-query-string into the "MyURI" variable.
RewriteCond %{ENV:MyURI}>%{THE_REQUEST} ^>[A-Z]+\ /([^\ ]+)\ HTTP/
RewriteCond %1 ^([^?]*\?([^%]*(\%(25)*([^3].|.[^D]))*)*\%(25)*3D.*)$ [NC,OR]
RewriteCond %1 ^([^?]*\?([^%]*(\%(25)*([^2].|.[^6]))*)*\%(25)*26.*)$ [OR]
RewriteCond %1 ^(([^%]*(\%(25)*([^3].|.[^F]))*)*\%(25)*3F.*)$ [NC]
RewriteRule ^. - [NE,E=MyURI:%1]
#
# If any encoded question mark is present in the client-requested URI, and
# no unencoded question mark is present, replace the first encoded question
# mark, queue up a redirect, and then re-start mod_rewrite processing
RewriteCond %{ENV:MyURI} ^[^?]+$
RewriteCond %{ENV:MyURI} ^(([^%]*(\%(25)*([^3].|.[^F]))*)*)\%(25)*3F(.*)$ [NC]
RewriteRule ^. - [NE,E=MyURI:%1?%7,E=QRedir:Yes,N]
#
# If any encoded "=" sign follows the "?", replace it, queue
# up a redirect, and re-start mod_rewrite processing
RewriteCond %{ENV:MyURI} ^([^?]*\?([^%]*(\%(25)*([^3].|.[^D]))*)*)\%(25)*3D(.*)$ [NC]
RewriteRule ^. - [NE,E=MyURI:%1=%7,E=QRedir:Yes,N]
#
# If any encoded ampersand follows the "?", replace it, queue
# up a redirect, and then re-start mod_rewrite processing
RewriteCond %{ENV:MyURI} ^([^?]*\?([^%]*(\%(25)*([^2].|.[^6]))*)*)\%(25)*26(.*)$
RewriteRule ^. - [NE,E=MyURI:%1&%7,E=QRedir:Yes,N]
#
# If we get here, there are no more percent-encoded characters which can
# and should be replaced by the rules above, so do the external redirect
RewriteCond %{ENV:QRedir} =Yes [NC]
RewriteRule ^. http://www.example.com/%{ENV:MyURI} [NE,R=301,L]

在这里找到:http://forums.iis.net/t/1193309.aspx?需要帮助将 3F 转换为和 3D 转换为

答案2

您几乎肯定会收到 403 错误。错误的原因在于 ? 是 Windows 和 Linux 上禁用的文件/目录名称字符。这意味着当 Apache 尝试查找名为“/document/root/index.php?blah”的文件或目录(解码后)时,会导致 403 错误。这是在读取 .htaccess 文件之前,因此您无法使用 .htaccess 文件中的 mod_rewrite 来覆盖此 403 错误,也无法使用 .htaccess 文件中定义的 ErrorDocument 来捕获此错误。

捕获 %3f 的唯一方法是使用 mod_rewrite 或“VirtualHost”中的 ErrorDocument例如在 httpd-vhosts.conf 中(或者如果没有任何“Virtualhost”,则在主服务器配置中,例如在 httpd.conf 中)。

相关内容