【问题标题】:Redirecting old domian to new domain by .htaccess通过 .htaccess 将旧域重定向到新域
【发布时间】:2012-04-03 06:30:45
【问题描述】:

我正在使用 Apache Mod-Rewrite moduled 及其 .htaccess 文件将旧域迁移到新域。 我们拥有几乎相同的新域结构,其中包括

  1. 网址
  2. 数据库

除了域名,就像它是 www.oldurl.com 现在它像 www.newurl.com 这就是我在旧域的.htaccess 文件中拥有的内容

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]

以上设置似乎工作正常,除了在一种情况下,我们的旧域中的 URL 已被删除或结构已更改,因此在这种情况下,上述规则将不起作用。我开始知道要添加一些东西除了我上面描述的内容之外,在我的 .htaccess 文件中就像这样

RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$
RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
Redirect 301 /my-page http://www.newurl.com/your-page

我总共有 20 多个这样的 URL,我想知道我是否需要将所有 20 多个 URL 映射到新的 URL,并且我需要注意它们应该放入文件中的任何顺序。

我也想知道 Apache 将如何工作,它会查看每个映射的 URL 是否匹配?还是以其他方式工作?

【问题讨论】:

    标签: apache .htaccess redirect http-status-code-301


    【解决方案1】:

    Redirect 指令不会绑定到RewriteCond 条件,并且总是将 /my-page 重定向到 http://www.newurl.com/your- page,此外,mod_rewrite 优先于 mod_alias,因此 RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L] 规则在查看 Redirect 指令之前应用。但是,如果 .htaccess 文件位于 oldurl.com 和 newurl.com 域的文档根目录中,则在浏览器获取后将应用 Redirect 指令重定向到http://www.newurl.com/my-page,从而(再次)重定向到http://www.newurl.com/your-page

    因此,您将这些按什么顺序排列并不重要,因为首先应用了 mod_rewrite。如果您有 20 个 URL 需要重定向到新站点上的新 URL,则可以在各自的 Redirect 中枚举它们。否则,如果您不想让浏览器被重定向两次,您可以使用 mod_rewrite 引擎枚举它们:

    RewriteEngine On
    
    # redirect the changed URLs individually
    RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.oldurl.com$
    RewriteRule ^my-page$ http://www.newurl.com/your-page [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.oldurl.com$
    RewriteRule ^my-page2$ http://www.newurl.com/your-page2 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.oldurl.com$
    RewriteRule ^my-page3$ http://www.newurl.com/your-page3 [R=301,L]
    
    # Finally, redirect everything else as-is
    RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.oldurl.com$
    RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
    

    请注意,这里的顺序确实很重要。必须重复 HTTP_HOST 的 2 个条件有点难看,你可以通过使用 SKIP 来解决这个问题,但最好只是重复它们。但是,如果您有权访问您的服务器配置或虚拟主机配置,请查看RewriteMap Directive,它允许您在您的情况下创建旧 url 到新 url 的映射,并且您可以减少所有单独更改的 url 重写一个人:

    在您的服务器/虚拟主机配置中,如下所示:

    RewriteMap newurls txt:/path/to/file/map.txt
    

    /path/to/file/map.txt 看起来像这样:

    my-page your-page
    my-page2 your-page2
    my-page3 your-page3
    etc...
    

    您的组合规则如下所示:

    RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.oldurl.com$
    RewriteRule ^(.*)$ http://www.newurl.com/${newurls:$1} [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.oldurl.com$
    RewriteRule (.*)$ http://www.newurl.com/$1 [R=301,L]
    

    【讨论】:

    • 感谢您的宝贵意见,但不幸的是,我无法访问 server config 或 vhost config 文件(共享主机),所以我相信我必须安定下来以按照建议枚举 URL你。
    猜你喜欢
    • 2016-06-24
    • 1970-01-01
    • 2021-11-28
    • 2020-06-15
    • 2015-07-17
    • 2014-05-25
    • 2021-07-24
    • 2016-02-22
    • 2011-09-23
    相关资源
    最近更新 更多