问题描述
我工作的一个网站,它的CMS用于保存使用下划线作为一个单词分隔符新页面的URL。
I'm working on a site, and its CMS used to save new page urls using the underscore character as a word seperator.
尽管谷歌目前对待强调作为一个字分隔符,这将是苛刻的场地使用破折号,而不是搜索引擎优化的力量。
Despite the fact that Google now treats underscore as a word seperator, the SEO powers that be are demanding the site use dashes instead.
这是非常简单的CMS内的事,当然我可以改变保存在服务于CMS的MySQL数据库的所有现有的网址。
This is very easy to do within the CMS, and I can of course change all existing URLs saved in the MySQL database that serves the CMS.
我的问题就出在编写将301旧式下划线分开联系的新的风格复姓verstion htaccess的规则。
My problem lies in writing a .htaccess rule that will 301 old style underscore seperated links to the new style hyphenated verstion.
我使用的答案这在其他网站上堆栈溢出的问题成功,使用:
I had success using the answers to this Stack Overflow question on other sites, using:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
不过此CMS网站使用了大量的现有规则,生产清洁的网址,我不能得到这个工作,与现有的规则集结合使用。
However this CMS site uses a lot of existing rules to produce clean URLs, and I can't get this working in conjunction with the existing rule set.
的.htaccess目前看起来是这样的:
.htaccess currently looks like this:
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以下格式之一:
All page URLS are in one of the 3 following formats:
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的技能显然是没有达到标准。
I'm sure I am missing something obvious, but at this level my regex and mod_rewrite skills clearly aren't up to par.
任何想法将大大AP preciated!
Any ideas would be greatly appreciated!
推荐答案
只要看看你已经使用规则:
Just take the rule you were using already:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
......并把它的在的所有其他重写规则,一切都应该工作。
…and put it before all other rewrite rules, and everything should work.
此外,您使用的RewriteBase /
的两倍。你可以忽略它的第二次,因为它已经定义。
Also, you used RewriteBase /
twice. You can omit it the second time, since it’s already defined.
这篇关于如何使用的.htaccess重写规则改变下划线以破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!