Wordpress - moving a site to another domain, 301 redirects

痴心易碎 提交于 2019-12-23 02:45:40

问题


I moved a wordpress site to another domain. I want to preserve the google links however and help it re-index the new site.

I did a 301 redirect but it only works for the root director. When someone goes to http://oldsite.com/directory/post-name-here/ I want them to be forwarded to http://newsite.com/directory2/post-name-here/. Both websites are in subdirectories. Is there a way to do it with in PHP? Or would I need a mod-rewrite?


What finally worked for me was this:

My oldsite file structure is like so:

/public_html/
     index.php
     /development/
         .htaccess

To recap, I wanted to forward everything coming into the development directory to the new website. This is the code of the .htaccess in the development directory.

<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]

Thank you so much to Peter


回答1:


This has always worked for me:

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



回答2:


You have to add this rule to .htaccess file see below.

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



回答3:


If you have only one link under the directory. You can add a index.php file to http://oldsite.com/directory/ with this code.

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

If you have many links under the directory, then use the following code

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

Here, we check if the file name is the old link, then redirect to new domain.



来源:https://stackoverflow.com/questions/38809503/wordpress-moving-a-site-to-another-domain-301-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!