Apache .htaccess: How to rewrite backslash with slash on Firefox?

孤街浪徒 提交于 2019-12-06 06:04:44

问题


How to rewrite backslash'\'with slash'/'on Firefox?

Chrome, IE, Safari, Opera has build browser rewrite backslash with slash.
But Firefox 3.6.13 returns 404 error page.

# Why Firefox returns 404 error page?
RewriteCond %{REQUEST_URI} (.*)\\(.*)
RewriteRule .* %1/%2 [R=301,L]

回答1:


It is Apache and FF bug, https://issues.apache.org/bugzilla/show_bug.cgi?id=35256
Hopefully it'll be fixed in soon feature.

  1. AllowEncodedSlashes should really be "on" by default and probably even deprecated. ...
  2. Nowhere the RFCs is a backslash (\) listed as a reserved character. Therefore a %5C
    should always be decoded the same as %7E is converted to a tilde (~).

To solve it on Apache:
add AllowEncodedSlashes On in VirtualHost httpd-vhosts.conf or httpd.conf, and .htaccess:

RewriteEngine On 
RewriteCond %{REQUEST_URI} ^(.*)\\(.*)$
RewriteRule .* %1/%2 [R=301,NC,L]



回答2:


Surprisingly, that seems to be correct behavior. Backslashes are not amongst the characters allowed in a HTTP or HTTPS URL (as per RFC 1738), therefore they should be escaped. Note that the RFC explicitly mentions backslash as an unsafe character:

Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are "{", "}", "|", "\", "^", "~", "[", "]", and "`".

All unsafe characters must always be encoded within a URL.

In other words, Firefox is doing the correct thing, even if it breaks pages which are incorrectly using backslashes (mostly from confusing URL syntax with Windows path syntax). Other browsers are trying to read the page author's mind, and they convert the backslashes to forward slashes before sending the request; whether this is a Good Thing is a matter of opinion.

Did you try to match on the escaped version of the backslash - %5C ?



来源:https://stackoverflow.com/questions/4887928/apache-htaccess-how-to-rewrite-backslash-with-slash-on-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!