【问题标题】:301 redirect to the rewrited url301重定向到重写的url
【发布时间】:2016-05-11 21:54:29
【问题描述】:

我正在使用的 CMS 具有以下重写规则:

对于标签:

RewriteRule ^tags/([^/]+)/$ tag.php?t=$1&page=1
RewriteRule ^tags/([^/]+)/page-([0-9]+)(/)?$ tag.php?t=$1&page=$2

对于分类页面:

RewriteRule ^browse-(.*)-videos.html$ category.php?cat=$1
RewriteRule ^browse-(.*)-videos-([0-9]+)-(.*).html$ category.php?cat=$1&page=$2&sortby=$3

但它不会重定向到 seo 友好的 url 结构创建重复的内容问题,我怎样才能让它也 301 重定向到重写的 url,如果从另一个访问?

提前致谢

【问题讨论】:

    标签: regex .htaccess redirect mod-rewrite


    【解决方案1】:

    您需要创建 4 条新规则并将它们置于现有规则之上。

    #redirect old URL to new SEO
    RewriteCond %{THE_REQUEST} ^GET\ /+tag\.php\?t=([^&\s]+)&page=1
    RewriteRule ^ /tags/%1/? [R=301,L]
    #redirect old URL to new SEO
    RewriteCond %{THE_REQUEST} ^GET\ /+tag\.php\?t=([^&\s]+)&page=([^&\s]+)
    RewriteRule ^ /tags/%1/page-%2/? [R=301,L]
    #redirect old URL to new SEO
    RewriteCond %{THE_REQUEST} ^GET\ /+category\.php\?cat=([^&\s]+)&page=([^&\s]+)&sortby=([^&\s]+)
    RewriteRule ^ /browse-%1-videos-%2-%3.html? [R=301,L]
    #redirect old URL to new SEO
    RewriteCond %{THE_REQUEST} ^GET\ /+category\.php\?cat=([^&\s]+)
    RewriteRule ^ /browse-%1-videos.html? [R=301,L]
    
    RewriteRule ^tags/([^/]+)/$ tag.php?t=$1&page=1 [L]
    RewriteRule ^tags/([^/]+)/page-([0-9]+)(/)?$ tag.php?t=$1&page=$2 [L]
    
    RewriteRule ^browse-(.*)-videos.html$ category.php?cat=$1 [L]
    RewriteRule ^browse-(.*)-videos-([0-9]+)-(.*).html$ category.php?cat=$1&page=$2&sortby=$3 [L]
    

    【讨论】:

    • 不起作用,它重定向到 domain.com/xxxx/public_html/browse--videos.html
    • 是的,我的错。我使用了错误的反向引用。此外,您的服务器显然需要在 URI 之前添加 /。尝试更新的规则。在尝试我更新的规则之前清除浏览器缓存。
    • 有了这个新规则,标签可以完美运行,但类别不能,例如:domain.com/category.php?cat=fun 重定向到 domain.com/browse-fun-videos.html 而应该重定向到 domain.com/browse-fun-videos-1-date.html... domain.com/category.php?cat=fun&page=2 也重定向到 domain.com/browse-fun-videos.html 而正确将是 domain.com/browse-fun-videos-2-date.html 和 domain.com/category.php?cat=fun&page=1&sortby=title 和 videos-1-title.html 和 domain.com/category.php? cat=fun&page=2&sortby=title 重定向到同一个 domain.com/browse-fun-videos.html
    • 而正确的重定向是 domain.com/browse-fun-videos-1-title.html 和 domain.com/browse-fun-videos-2-title.html
    • 可能最简单的解决方法是更改​​规则的顺序。清除缓存并尝试更新。
    猜你喜欢
    • 2011-01-18
    • 2013-12-21
    • 2015-03-04
    • 2011-09-01
    • 1970-01-01
    • 2016-09-28
    • 2023-03-25
    • 2013-01-27
    • 1970-01-01
    相关资源
    最近更新 更多