gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:40:49 26 4
gpt4 key购买 nike

我在一个网站上工作,它的 CMS 用于使用下划线字符作为单词分隔符来保存新页面 url。

尽管 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"

所有页面 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/。您可以第二次省略它,因为它已经定义了。

关于apache - 如何使用 .htaccess RewriteRule 将下划线更改为破折号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2637004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com