2014-09-06 69 views
2

我想重寫像使用的.htaccess文件以下網址:如何使用.htaccess文件或同等文件從以下URL中刪除「detail.html」?

讓他們爲改寫:

http://www.example.com/haunted_pub 
http://www.example.com/haunted_pub/west_and_south_west 
http://www.example.com/haunted_pub/west_and_south_west/wiltshire 
http://www.example.com/haunted_pub/west_and_south_west/wiltshire/bradford_on_avon 
http://www.example.com/haunted_pub/west_and_south_west/wiltshire/bradford_on_avon/6149/the_cross_guns 

是否有一個規則,我可以用它來簡單地截斷來自任何URL的最終「detail.html」?如果是這樣,那麼在這種情況下會是什麼?

編輯 這是在IIS下運行。

總之,如果我要求:http://www.example.com/haunted_pub/west_and_south_west然後我希望服務器來渲染http://www.example.com/haunted_pub/west_and_south_west/detail.html但在地址欄中顯示爲http://www.example.com/haunted_pub/west_and_south_west

回答

2

保的名字,你可以把這段代碼在你的htaccess

RewriteEngine On 

RewriteCond %{THE_REQUEST} \s/(.+?)/detail\.html\s [NC] 
RewriteRule . /%1 [R=301,L] 

RewriteCond %{DOCUMENT_ROOT}/$1/detail\.html -f 
RewriteRule ^(.+)$ /$1/detail.html [L] 

編輯

由於您在IIS服務器上,您不能使用h taccess文件(僅適用於Apache服務器)。
您需要在您的案例中的web.config文件中使用特定的重寫語法。
但我認爲它存在某種模擬器來啓用IIS服務器上的htaccess。
或者你也可以使用類似isapirewrite

+1

那是快 - 讓我來試試 – CarneyCode 2014-09-06 10:17:56

+0

http://www.example.com/haunted_pub/west_and_south_west給出了一個500錯誤 - IISPassword:該頁面無法顯示 – CarneyCode 2014-09-06 10:32:07

+0

這是另一個故事,因爲'/ detail.html'被刪除。你當然有一個更好的位置,知道爲什麼這個網址不起作用 – 2014-09-06 10:35:46