2012-11-02 33 views
2

我如何取代+到 - 在我的網址。在我的htacces中添加什麼代碼來擺脫+,並用減號代替它。重寫規則url字符替換

RewriteCond %{QUERY_STRING} ^search=([^&]+)$ 
RewriteRule^http://mysite.com\/Download\/free\/%2.html? [R,L,NE] 

如果鍵入包含空格的搜索,每一個空間變得+,我想 - 。

+0

哪裏'+'出現在URL?爲什麼你有重寫規則不能解決你的問題呢? –

+0

http://mysite.com/Download/free/bla+bla+bla.html因爲這是我在我的.htacces文件。 –

回答

4

我假設BLA BLA + +的URL的BLA部分:http://mysite.com/Download/free/bla+bla+bla.html源於查詢字符串search=的價值。根據您可能擁有的其他規則,您可以通過兩種不同的方式來解決這個問題。您可以首先刪除查詢字符串中的所有空格,然後將其重定向到.html文件。或者,您可以將查詢字符串重寫爲URI,然後刪除重定向之前的空格。這將是這樣的:

RewriteCond %{QUERY_STRING} ^search=(.*?)(\+|%20)(.*)$ 
RewriteRule^/?search=%1-%3 [L,NE] 

RewriteCond %{QUERY_STRING} !(\+|%20) 
RewriteCond %{QUERY_STRING} ^search=([^&]+)$ 
RewriteRule^http://mysite.com\/Download\/free\/%1.html? [R,L,NE] 

注意,在你的htaccess你有%2後向引用,這似乎沒有任何引用。

或重寫第一個URI,然後重定向:

RewriteCond %{QUERY_STRING} ^search=([^&]+)$ 
RewriteRule^/Download\/free\/%1.html? [L,NE] 

RewriteCond %{REQUEST_URI} !(\) 
RewriteRule^- [L,R] 

RewriteRule ^(.*)\ (.*)$ /$1-$2 [L] 
+1

第一個選項適用於我。我在這個問題上搜索了一個好的答案,1個月。非常感謝。 –