Htaccess - 将下划线组合成连字符,将大写字母组合成小写字母 301 重定向

Htaccess - Combining underscore to hyphen and uppercase to lowercase 301 redirect

目前我在我的网站上使用连字符 301 下划线,这很好用,但我也想将所有链接转换为小写并添加大写到小写重写。

我可以做到这一点,但使用当前方法它会执行以下操作,我认为这对 SEO 不是很好:

  1. 原版URL
  2. 301
  3. 已替换连字符
  4. 301
  5. 小写连字符 URL

如何将以下重写组合到一个查询中,以便通过 301 将带有下划线或大写字母的 url 转换为连字符和小写字母?

重写下划线到连字符

RewriteRule ^post/([^_]*)_([^_]*_.*)$ /posts/new-category-1/- [L,NE]
RewriteRule ^post/([^_]*)_([^_]*)$ /posts/new-category-1/- [L,NE,R=301]

RewriteRule ^forum/([^_]*)_([^_]*_.*)$ /forums/new-category-1/- [L,NE]
RewriteRule ^forum/([^_]*)_([^_]*)$ /forums/new-category-1/- [L,NE,R=301]

大写到小写重写

httpd.conf

RewriteMap lc int:tolower

.htaccess

RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}} [L,R=301]

您可以通过这样的规则避免多个 301

RewriteRule ^(post|forum)/([^_]*)_([^_]*_.*)$ //- [L,NE]

# if there is any upper case letter then do both lowercase conversion AND
# underscore to hyphen replacement
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(post|forum)/([^_]*)_([^_]*)$ /${lc:}/${lc:}-${lc:} [L,NE,R=301]

# otherwise regular underscore to hyphen replacement
RewriteRule ^(post|forum)/([^_]*)_([^_]*)$ //- [L,NE,R=301]

# and regular lower case conversion
RewriteRule ^(post|forum)/[^A-Z]*[A-Z] ${lc:%{REQUEST_URI}} [L,R=301]