Hello在我的项目中,我有以下htaccess。
一切正常,例如我输入以下网址domain.com/ test some test / test /
成为
domain.com/test+some+test/test
正如预期的那样
至少对我来说奇怪的是我将domain.com/test+some+test/test
编辑为
domain.com/test+some+test /test
然后再次按回车然后它会导致:
domain.com/test+some+test%20%20%20%20%20/test
不应该再次逃脱吗?如果我遗漏了什么,请指出。
Options All -Indexes +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# remove spaces from start or after /
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
# remove spaces from end or before /
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
# replace spaces by + in between
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1+$2 [L,R=301]
# Remove trailing slash
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
# Add trailing slash
#RewriteCond %{REQUEST_URI} !(/$|\.)
#RewriteRule (.*) %{REQUEST_URI}/ [L,R=301]
# Remove multiple slashes
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/{2,} [NC]
RewriteRule ^(.*) $1 [R=301,L]
# Clean url rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(png|jpg|jpeg|bmp|gif|css|js|json)$ [NC]
RewriteRule ^(.*)$ /index.php?req=$1 [L,QSA]
</IfModule>
更新
经过进一步调查并按照Anubhava的建议在前2个重写上应用R开关只剩下1个问题
如果我输入http://tms.localhost/test+some+test /test
,它就变为http://tms.localhost/test+some+test%20%20%20/test
但是如果我像http://tms.localhost/test some test /test
那样输入它就会变为预期http://tms.localhost/test+some+test/test
答案 0 :(得分:1)
在应用任何重写规则之前,编码为+
或%20
的空格将解码。这意味着这个正则表达式模式:[\s%20]
匹配空格,百分号,2和0。
正则表达式只需要:
# remove spaces from start or after /
RewriteRule ^(.*/|)[\s]+(.+)$ $1$2 [L]
# remove spaces from end or before /
RewriteRule ^(.+?)[\s]+(/.*|)$ $1$2 [L]
问题是,如果浏览器请求:/test+test%20test/
或/test%20test+test/
,则无论如何都会转移到(空格)。因此,您需要匹配实际请求:
# replace spaces by + in between
RewriteCond %{THE_REQUEST} \ /(.*?)(%20)+([^\?\ ]*)
RewriteRule ^ /%1+%3 [L,R=301,NE]
答案 1 :(得分:0)
在最初的2条规则中添加R
标志:
# remove spaces from start or after /
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L,R,NE]
# remove spaces from end or before /
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L,R,NE]