Regex htaccess在删除空白时的奇怪行为
您好,在我的项目中,我有以下访问权限。 一切正常,例如,我输入以下urlRegex htaccess在删除空白时的奇怪行为,regex,apache,.htaccess,mod-rewrite,clean-urls,Regex,Apache,.htaccess,Mod Rewrite,Clean Urls,您好,在我的项目中,我有以下访问权限。 一切正常,例如,我输入以下urldomain.com/test some test/test/ 变成 domain.com/test+some+test/test 果然 对我来说,奇怪的事情至少是当我编辑domain.com/test+some+test/test到 domain.com/test+some+test /test 然后再次按enter键,结果如下: domain.com/test+some+test%20%20%20%20
domain.com/test some test/test/
变成
domain.com/test+some+test/test
果然
对我来说,奇怪的事情至少是当我编辑domain.com/test+some+test/test
到
domain.com/test+some+test /test
然后再次按enter键,结果如下:
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>
Options所有-索引+FollowSymLinks-多视图
#打开mod_rewrite
重新启动发动机
重写基/
#从开始或之后删除空格/
重写规则^(.*/|)[\s%20]+(.+)$$1$2[L]
#从末尾或前面删除空格/
重写规则^(.+?)[\s%20]+(/.*)$$1$2[L]
#将中间的空格替换为+
重写规则^([^\s%20]*)(?:\s|%20)+(.*)$$1+$2[L,R=301]
#删除尾部斜杠
重写规则^(+)/$http://%{http_HOST}/$1[L,R=301]
#添加尾部斜杠
#重写cond%{REQUEST_URI}!(/$|\.)
#重写规则(.*)%{REQUEST_URI}/[L,R=301]
#删除多个斜杠
重写cond%{THE_REQUEST}^[A-Z]{3,}\s/{2,}[NC]
重写规则^(.*)$1[R=301,L]
#干净的url重写
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写条件%{REQUEST_URI}!^.*\。(png | jpg | jpeg | bmp | gif | css | js | json)$[NC]
重写规则^(.*)$/index.php?req=$1[L,QSA]
更新
在进一步调查并按照Anubhava的建议在前2条重写规则上应用R开关后,只剩下1个问题
如果我输入
http://tms.localhost/test+some+test/test
变成http://tms.localhost/test+有些+测试%20%20%20/test
但是如果我像这样输入它http://tms.localhost/test 一些测试/测试
它会变成预期的http://tms.localhost/test+某些+test/test
空间编码为+
或%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]
在最初的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]
再次向你致敬:P这失败了Anubhava domain.com/hello+there+boy%20%20/test。Test:domain.com/hello+there+boy/Test(如您所见的3个空间)在您的浏览器中输入
http://domain.com/test+some+test/test
对吗?对,这就是我在建议的速度变化后在测试中使用的方法。该URL变成:http://localhost/test+some+test/test
为我。请确保在不同的浏览器中进行测试,因为您使用的是301。因此http://tms.localhost/test+现在唯一的问题是some+test/test
?您在浏览器中看到文本%20
了吗?@panagiotisgorgeraditsas它只将%20
替换为+
,我觉得这是您想要的。@panagiotisgorgeraditsas我编辑了正则表达式来处理多个%20
's@PanagiotisGeorgeRaditsas我不知道你说的是什么意思“它变成http://tms.localhost/test+some+test%20%20%20/test
”,因为这是浏览器本身的URL编码空间。它与服务器正在做什么无关。现在大多数浏览器将其编码为%20,然后将其作为请求发送。上面的代码检查%{u请求}
var查找该类型的编码,并重定向浏览器将其编码为+