我正在网站上工作,其CMS用于保存新的网页网址,使用下划线字符作为单词分隔符。
尽管Google现在将下划线视为单词分隔符,但SEO要求网站使用破折号。
这在CMS中非常容易,我当然可以更改为CMS提供服务的MySQL数据库中保存的所有现有URL。
我的问题在于编写一个.htaccess规则,该规则将301旧式下划线链接到新风格的连字符。
我在其他网站上使用this Stack Overflow question的答案成功,使用:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
但是,此CMS站点使用大量现有规则来生成干净的URL,而且我无法将其与现有规则集结合使用。
.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"
所有网页网址均采用以下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技能显然不符合标准。
任何想法都将不胜感激!
答案 0 :(得分:3)
只需采用您已使用的规则:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
...并将放在之前所有其他重写规则,一切都应该有效。
另外,您使用了RewriteBase /
两次。你可以第二次省略它,因为它已经定义了。