2010-02-25 37 views
1

我只是想從重寫所有的請求:初學者URL重寫htaccess的

http://example.com/products/product.cfm?id=product-name 

http://example.com/products/product-name 

其次,

http://example.com/category.cfm?id=some-category&sub=sub-category 

http://example.com/some-category/sub-category 

以下是我已經試過:

Options +FollowSymLinks 
RewriteEngine on 
RewriteRule ^products/$1 ^products/product.cfm?id=$1 [NC] 

我敢肯定,是沒有意義的,因爲我真的不知道我在做什麼。我希望有人能告訴我我要出錯的地方,所以我可以通過例子來追蹤。

謝謝! 喬治

+0

如果在這種情況下類別和產品的名稱相同,會發生什麼情況? – ehdv 2010-02-25 21:03:26

+0

寫錯了 - 更正。謝謝! – 2010-02-25 21:06:04

回答

0

使用mod_rewrite,你可以寫:

RewriteRule ^products/([\w-]+)$ /products.cfm?id=$1&%{QUERY_STRING} 

這會做你想要的翻譯,同時保留原始URL中的其他參數。它不會處理類別,但我不確定您是否可以像您所描述的那樣同時使用這兩個類別。

0

東西沿着這些路線:

RewriteCond %{query_string} &?id=([^&]+) [NC] 
RewriteRule ^/products/product.cfm$ /products%1? [R,NC,L] 

# Fragile, this assumes that params will always be in order id= then sub= 
RewriteCond %{query_string} &?id=([^&]+)&sub=([^&]+) [NC] 
RewriteRule ^/category.cfm?$ /%1/%2? [R,NC,L] 

在重寫規則的R標誌強制硬重定向(訪問者的地址欄將變成新的URL)。刪除此標誌以進行內部重定向。