How to redirect 301 entire site. 301 redirect in .htacess or PHP

This article was written by in January 16, 2015, & may not be posted on other sites!
Original source url for this article: How to redirect 301 entire site. 301 redirect in .htacess or PHP

How to redirect 301 entire site. 301 redirect in .htacess or PHP
When moving a website you should always use a 301 redirect to keep SEO, ranking, visitor rate and earnings intact. This means that you let your old website stay online a little while but is redirecting all traffic to the new website. Your new website will soon take over all worked in seo, rankings & earnings and after that you can safely remove your old website for good.

How to redirect 301 entire site. 301 redirect in .htacess or PHP

 

Why use 301 redirect?

Don’t just delete your old website once your new one is up and running. Using a 301 redirect when moving a website from one location to another has its benefits:

  • You will keep most of the SEO, search engine ranking & earnings and pass everything to the new website immediately.
  • Duplicate content will hurt your ranking in Google search results. By redirecting, only the new website is counted for in search results and ranking. Duplicate content is no longer an issue.
  • All search results in a Google search showing your old website links will automatically redirect the visitor into the new website.
  • It takes search engines like Google up to 3 months to fully index all pages in a larger website. Using a 301 redirect, all your indexed pages in your old website will automatically send the user to your new website.
  • Google will also index your new website faster using 301 redirect.
  • You can automatically send visitors to the new website and the right page without them even noticing.

 

Good to know before you start 301 redirect

If you are using the htaccess file for redirecting, you need to put the htaccess in the right location on your server. Upload the htaccess file inside the folder where you want to perform the redirect from. Examples:

  • Putting it right inside the html-root folder “public_html” or “www”, the starting point for all redirects will be the root: www.yourwebsite.com.
  • Putting the htaccess file in a subfolder for example www.yourwebsite.com/subfolder then “www.yourwebsite.com/subfolder” will be the starting point for all redirects.
  • Redirect 301 / = redirect from your htaccess location, no need to write “www.yourwebsite.com” or “www.yourwebsite.com/subfolder” just start with /.

 

How to redirect 301 entire site, but keeping all url paths the same

Use this 301 redirect if you moved a whole website from www.old-domain.com to www.new-domain.com and all of the subpages and content have identical url paths on both websites. This means that the visitor will be sent from www.old-domain.com/subpage to www.new-domain.com/subpage. Add this code in your .htaccess on your old domain to forward users and keeping url paths the same when using 301 redirect:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.new-domain.com$1 [R=301,L]

How to 301 redirect but keeping all url paths using PHP
<?php
if ($_SERVER['HTTP_HOST'] != 'www.new-domain.com') {
header('HTTP/1.1 301 Moved Permanently');
header("Location: http://www.new-domain.com".$_SERVER['REQUEST_URI']);
die();
}
?>

 

How to 301 redirect a single url

To 301 redirect a singel url using 301 redirect, add this code to your .htaccess on your old domain to forward visitors to the new page.
Redirect 301 /old-page.html http://www.new-website.com/new-page.html

If the above is not working for some reason, try this code:
RewriteRule ^old-page.html$ http://www.new-website.com/new-page.html [R=301,L]

How to 301 redirect a single url using PHP
Put this php code on the page you wish to forward and add your new location.
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://new-website.com/");
die();
?>

 

Redirect 301 entire site to one single page

This 301 redirect code will send the visitor from any page on the old website to the index page of the new website. Put this code inside the .htaccess on your old domain.
Redirect 301 / http://www.new-website.com/

Using php, put this code on every page you wish to forward
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.new-website.com");
?>

 

Changing a specific path when url don’t quite match, using 301 redirect

If the path doesn’t quite match on your old website vs your new website, use this code to change any given path:
RedirectMatch 301 ^/some-old-path/(.+)$ http://www.new-website.com/new-path/$1

 

Moving from “http://www” to “http://” using 301 redirect

Moving from http://www.old-website.com to http://new-website.com use this code inside your .htaccess file of the old website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST}//s%{HTTPS} ^www.(.*)//((s)on|s.*)$ [NC]
RewriteRule ^ http%3://%1%{REQUEST_URI} [L,R=301]

Using PHP to redirect from www to http://
<?php
if ($_SERVER["HTTP_HOST"] == "www.new-domain.com") {
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: www.new-domain.com".$_SERVER['REQUEST_URI']);
die();
}
?>

 

Moving from “http://” to “http://www” using 301 redirect

Moving from http://old-website.com to http://www.new-website.com use this code inside your .htaccess file of the old website:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Using PHP to redirect from http:// to http://www
<?php
if ($_SERVER["HTTP_HOST"] == "new-domain.com") {
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.new-domain.com".$_SERVER['REQUEST_URI']);
die();
}
?>

 

The difference between 301 redirect and 302 redirect

Use 301 redirect
if your website is moving permanently to have search engines like Google also redirecting most of the worked in ranking & SEO.
Use 302 redirect
if your website is moving temporary to keep the SEO and ranking on your old website.

 

Spread the word

Facebooktwittergoogle_pluslinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *