2014-03-30 29 views
0

我的所有URL格式是這樣的:有沒有辦法通過htaccess通過刪除URL的parat來更改URL以清除URL?

http://example.com/category?format=feed 
http://example.com/category/sub_category/?format=feed 

例如:http://example.com/computer/cpu/?format=feed

是否有通過htaccess的辦法改變的熱門網址這些:

http://example.com/category/feed 
http://example.com/category/sub_category/feed 

例1:http://example.com/computer/cpu/feed

示例2:http://example.com/computer/feed

+0

是的,有一種方法。查看右側的長列表以獲取更多信息 - > – PeeHaa

回答

2

這些規則應該同時滿足這兩個要求。但只要正常的URL不會更改,並允許您更改類別和子庫,並且format始終是查詢字符串中的關鍵字。

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)/(.+)/(.+)$ /$1/$2/?format=$3 [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)/(.+)$ /$1/?format=$2 [L] 

然後你應該可以使用這些URL類型。

http://example.com/category/feed 
http://example.com/category/sub_category/feed 
+0

感謝您的回答......乾淨的URL只能在內部更改......是嗎?但沒有發生! – user3307827

+0

是的,像這個'http:// example.com/category/feed'乾淨的URL是地址欄中應該顯示的內容。如果你使用上面的示例代碼,它應該只在內部重定向。這個'[L]'告訴它在匹配規則時停止處理。確保這是你在那裏的一切。不要使用'R'。 –

0

下應該達到你在找什麼:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} ^format=(.*)$ 
RewriteRule ^(.*)/(.*)/$ /$1/$2/%1? [L,R] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{QUERY_STRING} ^format=(.*)$ 
RewriteRule ^(.*)$ /$1/%1? [L,R] 

以上會變成:

http://example.com/category?format=feedhttp://example.com/category/feed

http://example.com/category/sub_category/?format=feedhttp://example.com/category/sub_category/feed