Saturday, December 1, 2012

.htaccess correct Page to Page 301 Redirect

Hi guys,

This will be a short post on how to permanently redirect your old site to a new site using .htaccess.

If you want to move your site to a new domain the proper .htaccess redirect should look something like this:

For the index page use the following redirect:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.OLDSITE\.com$ [OR]
RewriteCond %{HTTP_HOST} ^OLDSITE\.com$
RewriteRule ^/?$ "http\:\/\/www\.NEWSITE\.com" [R=301,L]
</IfModule>

What this redirect will do is only redirect the old index page with the new index page, so when someone types www.oldsite.com it will open www.newsite.com.

If you want to redirect the old site to a sub directory on the new site you should use the following .htaccess redirect:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.OLDSITE\.com$ [OR]
RewriteCond %{HTTP_HOST} ^OLDSITE\.com$
RewriteRule ^/?$ "http\:\/\/www\.NEWSITE\.com\/OLDSITE\.html" [R=301,L]
</IfModule>

Now you want to redirect all other pages right? Here is how it's done:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^OLDPAGE\.html$ http://www.NEWSITE.com/OLDPAGE.html [R=301]
</IfModule>

At the end it should look something like this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.OLDSITE\.com$ [OR]
RewriteCond %{HTTP_HOST} ^OLDSITE\.com$
RewriteRule ^/?$ "http\:\/\/www\.NEWSITE\.com" [R=301,L]
RewriteRule ^OLDPAGE1\.html$ http://www.NEWSITE.com/OLDPAGE1.html [R=301]
RewriteRule ^OLDPAGE2\.html$ http://www.NEWSITE.com/OLDPAGE2.html [R=301]
RewriteRule ^OLDPAGE3\.html$ http://www.NEWSITE.com/OLDPAGE3.html [R=301]
RewriteRule ^OLDPAGE4\.html$ http://www.NEWSITE.com/OLDPAGE3.html [R=301]
</IfModule>

Do this for all pages and keep the content intact or very close to the old site.
Keep the old files until you notice that all pages are indexed correctly and all visitors are redirected correctly.


No comments:

Post a Comment