2010-04-14 75 views
3

我正在使用網站,其CMS使用下劃線字符作爲單詞分隔符來保存新的頁面URL。如何使用.htaccess RewriteRule將下劃線更改爲破折號

儘管Google現在將下劃線當作單詞分隔符,但是SEO功能卻要求網站使用破折號。

這在CMS中很容易實現,我當然可以更改保存在爲CMS提供服務的MySQL數據庫中的所有現有URL。

我的問題在於編寫一個.htaccess規則,將301舊風格強調分離的鏈接到新式複合版本。

我使用的答案成功this Stack Overflow question其他網站,使用:

RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N] 
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301] 

但是這個CMS網站使用了大量的現有規則,生產清潔的網址,我不能得到結合這個工作與現有的規則集合。

的.htaccess目前看起來是這樣的:

Options FollowSymLinks 
# RewriteOptions MaxRedirects=50 

RewriteEngine On 
RewriteBase/

RewriteCond %{HTTP_HOST} !^www\.mydomain\.co\.uk$ [NC] 
RewriteRule (.*) http://www.mydomain.co.uk/$1 [R=301,L] 

#trailing slash enforcement 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} !# 
RewriteCond %{REQUEST_URI} !(.*)/$ 
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1/ [L,R=301] 

RewriteRule ^test/([0-9]+)(/)?$ test_htaccess.php?year=$1 [nc] 

RewriteRule ^index(/)?$ index.php 

RewriteRule ^department/([^/]*)/([^/]*)/([^/]*)(/)?$ ecom/index.php?action=ecom.details&mode=$1&$2=$3 [nc] 
RewriteRule ^department/([^/]*)(/)?$     ecom/index.php?action=ecom.details&mode=$1 [nc] 

RewriteRule ^product/([^/]*)/([^/]*)/([^/]*)(/)?$ ecom/index.php?action=ecom.pdetails&mode=$1&$2=$3 [nc] 
RewriteRule ^product/([^/]*)(/)?$     ecom/index.php?action=ecom.pdetails&mode=$1 [nc] 
RewriteRule ^content/([^/]*)(/)?$     ecom/index.php?action=ecom.cdetails&mode=$1 [nc] 

RewriteRule ([^/]*)/action/([^/]*)/([^/]*)/([^/]*)/([^/]*)(/)?$ $1/index.php?action=$2&mode=$3&$4=$5 [nc] 
RewriteRule ([^/]*)/action/([^/]*)/([^/]*)(/)?$     $1/index.php?action=$2&mode=$3 [nc] 
RewriteRule ([^/]*)/action/([^/]*)(/)?$       $1/index.php?action=$2 [nc] 
RewriteRule ^eaction/([^/]*)/([^/]*)/([^/]*)/([^/]*)(/)?$  ecom/index.php?action=$1&mode=$2&$3=$4 [nc] 
RewriteRule ^eaction/([^/]*)/([^/]*)(/)?$      ecom/index.php?action=$1&mode=$2 [nc] 
RewriteRule ^action/([^/]*)/([^/]*)(/)?$      index.php?action=$1&mode=$2 [nc] 
RewriteRule ^sid/([^/]*)(/)?$         index.php?sid=$1 [nc] 




## Error Handling ## 
#RewriteRule ^error/([^/]*)(/)?$ index.php?action=error&mode=$1 [nc] 

# ----------------------------------- Content Section ------------------------------ # 
#RewriteRule ^([^/]*)(/)?$ index.php?action=cms&mode=$1 [nc] 

RewriteRule ^accessibility(/)?$ index.php?action=cms&mode=accessibility 
RewriteRule ^terms(/)?$   index.php?action=cms&mode=conditions 
RewriteRule ^privacy(/)?$  index.php?action=cms&mode=privacy 
RewriteRule ^memberpoints(/)?$ index.php?action=cms&mode=member_points 

RewriteRule ^contactus(/)?$ index.php?action=contactus 
RewriteRule ^sitemap(/)?$ index.php?action=sitemap 

ErrorDocument 404 /index.php?action=error&mode=content 

ExpiresDefault "access plus 3 days" 

所有頁面URL是在3種以下格式之一:

http://www.mydomain.com/department/some_page_address/ 

http://www.mydomain.com/product/some_page_address/ 

http://www.mydomain.com/content/some_page_address/ 

我敢肯定,我失去了一些東西很明顯,但在這級別我的正則表達式和mod_rewrite技能顯然沒有達到標準。

任何想法將不勝感激!

回答

3

只取規則你使用已經:

RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N] 
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301] 

...並把它之前所有其他重寫規則,一切都應該工作。

此外,您使用了兩次RewriteBase /。您可以第二次省略它,因爲它已經被定義。

+0

感謝那Mathias - 我實際上曾嘗試過,並沒有意識到它的工作原因是錯誤頁面在我的CMS上的工作方式。 非常感謝! – soopadoubled 2010-04-19 11:42:29

+0

@ mathias-bynens,如何從這個規則中排除圖像文件? – 2014-03-06 16:30:31

相關問題