Wordpress - moving a site to another domain, 301 r

2019-06-01 17:15发布

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

3条回答
相关推荐>>
2楼-- · 2019-06-01 17:21

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-06-01 17:25

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]
查看更多
看我几分像从前
4楼-- · 2019-06-01 17:35

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]
查看更多
登录 后发表回答