2015-03-19 100 views
1

我正在嘗試用下劃線重寫網址以便爲購物網站劃線。將單個和連續的下劃線重寫爲破折號

產品名稱目前對空格有下劃線。一個典型的網址如下:

http://test.local/category-name/product_name_a

我目前的.htaccess如下,併成功地重寫這:

http://test.local/category-name/product-name-a

RewriteEngine On 
RewriteBase/

#Rewrite Underscores to dashes 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^_]*)_([^_]*)$ $1-$2 [L,R=302,NE] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^_]*)_+(.+)$ $1-$2 [L] 

# Redirect to index.php & standard Opencart stuff 
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L] 
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L] 
RewriteRule ^download/(.*) /index.php?route=error/not_found [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) 
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA] 

但是有些產品有連續的下劃線在他們這樣的as

http://test.local/category-name/product__name__b

或混合兩種,如:

http://test.local/category-name/product__name_a

我已經嘗試和失敗,讓這些改寫到:

http://test.local/category-name/product--name--b

http://test.local/category-name/product--name-a

不破現有的重寫。任何幫助非常感謝。

回答

1

只需調整你的正則表達式中的第二個規則:

RewriteEngine On 
RewriteBase/

#Rewrite Underscores to dashes 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^_]*)_([^_]*)$ $1-$2 [L,R=302,NE] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^_]*)_(.+)$ $1-$2 [L] 

# Redirect to index.php & standard Opencart stuff 
RewriteRule ^sitemap\.xml$ index.php?route=feed/google_sitemap [L] 

RewriteRule ^googlebase\.xml$ index.php?route=feed/google_base [L] 

RewriteRule ^download/(.*) /index.php?route=error/not_found [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) 
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA] 
+1

完美,謝謝! – jx12345 2015-03-19 13:00:04

+0

由於此更改將是永久性的,因此使用301重定向會更好嗎?並通過編輯第一條規則來實現:RewriteRule ^([^ _] *)_([^ _] *)$ $ 1- $ 2 [L,R = 301,NE] – jx12345 2015-03-19 13:52:36

+1

是繼續, 301規則,因爲一切工作正常。 – anubhava 2015-03-19 13:58:59

相關問題