如何使用.htaccess RewriteRule将下划线更改为破折号

问题描述:

我正在使用网站,其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技能显然没有达到标准。

任何想法将不胜感激!

只取规则你使用已经:

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