2013-09-21 86 views
3

我很努力地完成这项工作。此刻我的htaccess包含以下代码:htaccess清理网址并用空格和%20替换 -

#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件事巨大的,如果我尝试这个网址,例如:

http://www.domain.com/ test/ 

浏览器翻译它想:以后http://www.domain.com/%20test/ 基本如果路径以空白或%20开头,则失败。 任何人都可以请指出一个解决方案的起始空间将被删除?

UPDATE

目标:

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/hello-there

回答

9

我有罪编写代码超过2年回来的:P

可以通过这个代码巨大简化:

# 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:必须添加你需要修复的源这些网址也是因为获取这样的网址确实不正常。

+0

是的我在这里看到了关于你的答案的代码在stackoverflow。 有没有办法彻底删除任何开始的空格? 例如:www.domain.com/测试成为www.comain.com/-a-test。是否有可能使www.domain.com/a-test? – Syd

+0

感谢你提出这个问题,因为在新的Apache版本中不需要'DPI'标志。 – anubhava

+0

也www.domain.com/你好那里/成为www.domain.com/----hello-there// – Syd