【问题标题】:Change old dynamic urls to new format with 301 redirect使用 301 重定向将旧的动态 url 更改为新格式
【发布时间】:2016-08-26 07:23:25
【问题描述】:

谁能帮我将旧的动态 url 格式更改为新的动态 url 格式?

确切地说,我需要将下划线 _ 更改为连字符 - 在 url 中,但使用 301 重定向 在 htaccess 中重定向,例如:

  • example.com/posts/22_keyword/33_this_is_the_example_of_old_url.html

我希望它是:

  • example.com/posts/22-keyword/33-this-is-the-example-of-old-url.html

我知道如何在 php 中更改它,但我需要使其适用于从旧 url 格式到新的 301 重定向 在 htaccess 中,所以我不会失去在 google 上的排名。

这是当前的 htaccess 信息:

RewriteRule ^posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html   index.php?view=showad&adid=$7&cityid=$1 [QSA]

转换为:

  • posts/3_keyword/6_keyword/235530_this_is_the_example_of_old_url.html

我不熟悉 mod rewrite,我想这对于了解它的人来说有点容易,因为 所要做的就是从 _ 更改为 -

【问题讨论】:

    标签: .htaccess redirect seo


    【解决方案1】:

    您可以使用此递归规则将_ 转换为-

    RewriteEngine On
    
    # using internal rewrites replace all _ by -
    RewriteRule ^([^_]+)_(.+?\.html)$ $1-$2 [NC,DPI,E=underscores:1]
    
    # one final redirect to change URL in browser 
    RewriteCond %{ENV:underscores} =1
    RewriteRule ^([^_]+)$ /$1 [R=302,NE,L]
    

    【讨论】:

    • 您在浏览器中输入了什么网址,遇到了什么错误?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 2010-11-09
    相关资源
    最近更新 更多