Htaccess - 将下划线与连字符和大写字母组合为小写301重定向

时间:2015-01-31 13:22:46

标签: apache .htaccess redirect httpd.conf

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

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

  1. 原始网址
  2. 301
  3. Hyphen取代
  4. 301
  5. 连字符小写网址
  6. 如何将以下重写合并到一个查询中,以便使用下划线或大写的网址通过301转换为连字符和小写?

    下划线到连字符重写

    RewriteRule ^post/([^_]*)_([^_]*_.*)$ /posts/new-category-1/$1-$2 [L,NE]
    RewriteRule ^post/([^_]*)_([^_]*)$ /posts/new-category-1/$1-$2 [L,NE,R=301]
    
    RewriteRule ^forum/([^_]*)_([^_]*_.*)$ /forums/new-category-1/$1-$2 [L,NE]
    RewriteRule ^forum/([^_]*)_([^_]*)$ /forums/new-category-1/$1-$2 [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]
    

1 个答案:

答案 0 :(得分:2)

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

RewriteRule ^(post|forum)/([^_]*)_([^_]*_.*)$ /$1/$2-$3 [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:$1}/${lc:$2}-${lc:$3} [L,NE,R=301]

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

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