为什么Apache .htaccess会将非拉丁字符URL重写为urlencoded?怎么解决?
domain.com//контакты
返回domain.com/%25d0%25ba%25d0%25be%25d0%25bd%25d1%2582%25d0%25b0%25d0%25ba%25d1%2582%25d1%258b
domain.com/контакты//
返回domain.com/%2525d0%2525ba%2525d0%2525be%2525d0%2525bd%2525d1%252582%2525d0%2525b0%2525d0%2525ba%2525d1%252582%2525d1%25258b/
RewriteEngine On
# Rewrite multiple slashes with single slash after domain
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
# Rewrite multiple slashes with single slash in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
答案 0 :(得分:4)
好吧,我大多同意@akond。但是我做了一些测试,你可以强制执行apache而不是通过 NE 标记重写响应中的字符。所以你的行将是:
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,NE,L]
在我的第一次测试中,我没有注意到任何变化,但实际上似乎我的firefox已经发送了“好的”请求并激活了网址编码。所以响应也是url编码的。所以我尝试了 telnet :
没有[NE]:
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
GET //контакты HTTP/1.1
Host: myvhost.com
HTTP/1.1 302 Found
Date: Sat, 05 Feb 2011 21:14:13 GMT
Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.6 with Suhosin-Patch
Location: http://myvhost.com/%d0%ba%d0%be%d0%bd%d1%82%d0%b0%d0%ba%d1%82%d1%8b
(.. to be continued ..)
使用[NE]:
telnet 127.0.0.1 80
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
GET //контакты HTTP/1.1
Host: myvhost.com
HTTP/1.1 302 Found
Date: Sat, 05 Feb 2011 21:12:39 GMT
Server: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.6 with Suhosin-Patch
Location: http://myvhost.com/контакты
(.. to be continued ..)
所以在apache方面你可以做点什么,但我不确定它是否真的有用:-)大多数浏览器在点击apache之前已经编码了url。例如,这是来自我的telnet客户端和firefox的apache access.log中的“相同请求”:
GET //\xd0\xba\xd0\xbe\xd0\xbd\xd1\x82\xd0\xb0\xd0\xba\xd1\x82\xd1\x8b HTTP/1.1
GET /%D0%BA%D0%BE%D0%BD%D1%82%D0%B0%D0%BA%D1%82%D1%8B HTTP/1.1
答案 1 :(得分:0)
http://en.wikipedia.org/wiki/Percent-encoding
这就是原因。你无法改变这种行为。