2013-10-27 28 views
-1

我使用一些重寫規則爲論壇創建可讀的URL。例如,如果您發佈新主題的主題爲「您對這款新車有什麼看法?」我會做這樣的鏈接:forum/cars/51-Hey-what-do-you-think-of-this-new-car-?其中51是主題ID。使用.htacces改寫規則和特殊字符的問題

在我的.htaccess我用這個重寫規則:

RewriteEngine on 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^forum/([0-9-a-z]+)/?$ forum/list_topics.php?key=$1 [L] 
RewriteRule ^forum/([0-9-a-z]+)/([0-9]+)?$ forum/list_topics.php?key=$1&page=$2 [L] 
RewriteRule ^forum/([0-9-a-z]+)/([0-9]+)-(.*)/?$ forum/list_post.php?topic=$2 [L] 

所以這個工作正常,除非我發佈這些字符主題標題:+"*ç%&/()=?'^

我使用一個PHP函數來創建網址是:

$url_topic = str_replace(' ', '-', $url_topic); //replace space with - 
$url_topic = urlencode($url_topic);//encode url 

在這種情況下,$ url_topic值是:34-%2B%22%2A%C3%A7%25%26%2F%28%29%3D%3F%27%5E,但是當我點擊它,我有以下錯誤:

Not Found 

The requested URL /forum/cars/34-+"*ç%&/()=?'^ was not found on this server. 
Apache/2.2.22 (Ubuntu) Server at localhost Port 80 

我在想什麼?

感謝

回答

0

網址轉義字符串解碼你比較含蓄對與重寫規則中的字符串中。這意味着如果你捕獲,他們已經解碼!

國際海事組織,最好的解決方案是針對%{THE_REQUEST}進行捕獲,這仍然是以客戶端的方式進行URL編碼的。這需要1個addl RewriteCond。

第二個選項是使用[B]標誌重新轉義反向引用。我不是這個人的粉絲,因爲重新逃避並不是真正意義上的上下文在哪裏去的。

+0

嗨,感謝您的解釋,但可以添加一個我必須做的RewriteCond的例子?否則,我想我會簡單地從我的PHP字符串中刪除特殊字符。這是我認爲最好的解決方案 –