2013-02-10 85 views
0

我一定是個白癡,因爲我無法工作。Mod重寫刪除網址的組成部分

我有一個網址: www.site.com.au/products/product-name.html

我需要將這些重定向到: www.site.com.au/product-name .html

所有鏈接都是動態的,該文件夾不存在。我用什麼ReWrite規則來實現這個目標?

這是我到目前爲止有:

RewriteCond %{HTTP_HOST} ^(www|test)\.site\.com\.au 
RewriteCond %{REQUEST_URI} ^(/products/) 
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L] 

只需要將位添加到遠程/產品

感謝。

回答

0

好吧,我真的不知道Apache Rewrite是否知道確切的格式。但經過無事生非這些都是工作的結果:

# Lowercase all /products/ 
RewriteCond %{HTTP_HOST} ^(www)\.site\.com\.au 
RewriteCond %{REQUEST_URI} ^/products/.+\.html 
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteRule^${lc:%{REQUEST_URI}} [R=301,L] 

# Lowercase all /products/ and strip products/ subfolder 
RewriteCond %{HTTP_HOST} ^(www)\.site2\.com\.au 
RewriteCond %{REQUEST_URI} ^/products/.+\.html 
RewriteCond %{REQUEST_URI} [A-Z] 
RewriteRule ^products/(.+\.html)$ /${lc:$1} [R=301,L] 

感謝,

大教堂

0
RewriteRule ^products(/.*)$ http://www.site.com.au$1 [L, R=301] 

這取代你上市,除第一的RewriteCond一切(相匹配的域名,但如果你的虛擬主機只在這兩個領域的回答,您可以排除的RewriteCond簡化它)。

在Apache查看RewriteConds之前,RewriteRules首先被匹配,所以如果你可以在RewriteRule本身中進行匹配,它將大大簡化事情。只是以備將來參考,如果你確實需要在的RewriteCond來匹配,這將是這個樣子:

RewriteCond %{REQUEST_URI} ^/products(/.*)$ 
RewriteRule ^.*$ http://www.site.com.au%1 [L, R=301] 

注意%1匹配什麼是在括號中的RewriteCond主場迎戰$1匹配什麼在RewriteRule。

編輯:根據您的評論,以下修改應強制小寫。我自己並不需要這樣做,但通過this Apache documentation這是一個通過RewriteMap的內部函數。根據你的原始代碼,看起來你可能已經在其他地方擁有了RewriteMap定義。如果不是的話,我把它包括在這裏。

RewriteMap lc int:tolower 
RewriteCond %{REQUEST_URI} ^/products(/.*)$ [NC] 
RewriteRule ^.*$ http://www.site.com.au${lc:%1} [L, R=301] 
+0

謝謝,但似乎這並不包括液晶元件,但 - 我們需要小寫整個URL ... – Dom 2013-02-10 10:26:57

+0

不是我以前做過的事情,但我更新了我認爲會強制小寫的答案。 – 2013-02-10 18:02:27