2013-09-27 90 views
2

你好在我的項目中我有以下htaccess。 一切工作正常,例如我輸入以下網址domain.com/ test some test / test /htaccess在刪除空格上的奇怪行爲

成爲 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> 

UPDATE

經過進一步調查,並在第2周的RewriteRules應用R開關的建議通過Anubhava只有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

添加R標誌在你最初的2條規則也:

# 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] 
+0

Omg you again:P this failed Anubhava domain.com/你好+有+男童%20%20%20 /測試。測試:domain.com/hello+there+boy/test(3spaces,如你所見) – Syd

+0

在你的瀏覽器中輸入'http://domain.com/test+some+test/test'吧? – anubhava

+0

是的,在測試之前有3個空格,測試 – Syd

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/,這是會得到trasnlated到(空間),而不管。因此,你需要來匹配實際的請求,而不是:

# replace spaces by + in between 
RewriteCond %{THE_REQUEST} \ /(.*?)(%20)+([^\?\ ]*) 
RewriteRule^/%1+%3 [L,R=301,NE] 
+0

沒有工作,空間置換中間要麼 – Syd

+0

@PanagiotisGeorgeRaditsas,只替換'%20'帶'+',這在我看來你想要什麼。 –

+0

@PanagiotisGeorgeRaditsas我編輯了正則表達式來處理多個'%20'的 –