【问题标题】:Wordpress - moving a site to another domain, 301 redirectsWordpress - 将网站移动到另一个域,301 重定向
【发布时间】:2016-12-13 01:34:33
【问题描述】:

我将一个 wordpress 网站移至另一个域。但是,我想保留谷歌链接并帮助它重新索引新站点。

我做了一个 301 重定向,但它只适用于根目录。当有人去http://oldsite.com/directory/post-name-here/时,我希望他们被转发到http://newsite.com/directory2/post-name-here/。这两个网站都在子目录中。有没有办法在 PHP 中做到这一点?或者我需要一个mod-rewrite吗?


最终对我有用的是:

我的旧站点文件结构是这样的:

/public_html/
     index.php
     /development/
         .htaccess

回顾一下,我想将进入开发目录的所有内容转发到新网站。这是development 目录中.htaccess 的代码。

<IfModule mod_rewrite.c>

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

非常感谢彼得

【问题讨论】:

    标签: wordpress .htaccess redirect mod-rewrite


    【解决方案1】:

    这一直对我有用:

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

    【讨论】:

      【解决方案2】:

      您必须将此规则添加到 .htaccess 文件中,见下文。

      Options +FollowSymLinks
      RewriteEngine On
      RewriteBase /
      RewriteCond %{HTTP_HOST} !^OLDDOMAIN\.com$ [NC]
      RewriteRule ^(.*)$ http://NEWDOMAIN.com [R=301,L]
      

      【讨论】:

      • 感谢您的回答,但我已经尝试过了,但没有成功。它成功地重定向了一个目录,但不是所有的帖子,并且它没有正确地重定向到目录。
      【解决方案3】:

      如果目录下只有一个链接。您可以使用此代码将 index.php 文件添加到 http://oldsite.com/directory/

      header("location: http://newsite.com/directory2/post-name-here/", true, 301);
      

      如果目录下有很多链接,那么使用下面的代码

      if($_SERVER["REQUEST_URI"]=="/directory/post-name-here/"){
          header("location: http://newsite.com/directory2/post-name-here/", true, 301);
      }
      

      这里,我们检查文件名是否是旧链接,然后重定向到新域。

      【讨论】:

      • 感谢您的回答!我尝试用正则表达式和另一个 htaccess 来做这个,但是遇到了太多麻烦
      猜你喜欢
      • 2021-11-19
      • 2020-01-31
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2016-07-30
      • 2017-09-21
      • 2016-09-12
      相关资源
      最近更新 更多