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
If you have only one link under the directory. You can add a index.php file to http://oldsite.com/directory/ with this code.
If you have many links under the directory, then use the following code
Here, we check if the file name is the old link, then redirect to new domain.
You have to add this rule to .htaccess file see below.
This has always worked for me: