htaccess的干净的网址和放大器;代替空格和%20 - [英] htaccess clean urls & replacing whitespaces and %20 with -
问题描述
我strugling,使这项工作。此刻我的htaccess包含以下code:
I'm strugling to make this work. At the moment my htaccess contains the following code:
#Debugging - Error reporting
php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
#Commpression
<ifmodule mod_deflate.c="">
<filesmatch ".(js|css|html|png|jpg|jpeg|swf|bmp|gif|tiff|ico|eot|svg|ttf|woff|pdf)$"="">
SetOutputFilter DEFLATE
</filesmatch>
</ifmodule>
Options All -Indexes +FollowSymLinks -MultiViews
<IfModule mod_rewrite.c>
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
#RewriteCond %{THE_REQUEST} (\s|%20)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1-$2 [L,R=301,DPI]
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(png|jpg|bmp|gif|css|js)$ [NC]
RewriteRule ^([^/]+/?.+)$ /index.php?req=$1 [L,QSA]
</IfModule>
一切都很正常,除了1的事情,如果我尝试这个网址,例如:
Everything works great except 1 thing if I try this url for example:
http://www.domain.com/ test/
浏览器将其翻译喜欢: http://www.domain.com/%20test/一> 如果路径以一个空格或未能通过20%的域名后,基本。 任何人都可以请指向一个解决方案,起始位会被删除?
the browser translates it like to: http://www.domain.com/%20test/ basically after the domain if the path starts with a whitespace or a %20 it fails. can anyone please point to a solution where the starting spaces will be removed ?
更新
目标:
www.domain.com/ this is a test / hello there /
或
www.domain.com/ this is a test
到
www.domain.com/this-is-a-test /
或 www.domain.com/this-is-a-test /你好,还有
推荐答案
我有罪书面code超过2年前的:P
I am guilty of writing that code more than 2 years back :P
这可以通过这个code被无限简化为:
That can be hugely simplified by this code:
# 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]
PS:必须补充一点,你需要修复这些URL还的来源,因为它实在是不正常的情况下获得的URL像这样
PS: Must add that you need to fix the source of these URLs also because it is really not normal to be getting URLs like this.
这篇关于htaccess的干净的网址和放大器;代替空格和%20 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!