PHP Redirection Formula

Create a 301 redirect forcing all http requests to use either http://www.example.com or example.com:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

Redirect to example.com/index.php

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]

Redirect visitors to a new site
Create a 301 redirect for all http requests that are going to the old domain.

  • Example 1 – Redirect from oldexample.com to http://www.newexample.com
  • Example 2 – Redirect from oldexample.com to newexample.com
RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]

This is useful when you use http://www.newexample.com as your new domain name (see also this article about redirecting www and non-www domains). If not – use the code of example 2.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301]

301 Redirect: xyz-site.com to http://www.xyz-site.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.xyz-site.com$ [NC]
RewriteRule ^(.*)$ http://www.xyz-site.com/$1 [L,R=301]

301 Redirect: http://www.xyz-site.com to xyz-site.com

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^xyz-site.com$ [NC]
RewriteRule ^(.*)$ http://xyz-site.com/$1 [L,R=301]

301 Redirect: Redirecting Individual pages

Redirect 301 /previous-page.html http://www.xyz-site.com/new-page.html
These scripts should be placed in the .htaccess file. I chose to expose these .htaccess methods first since they are most flexible and powerful of them all. These examples alone serve as a great case for hosting on Linux web servers.

Redirect with PHP

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.xyz-site.com" );
exit(0);
?>

  • This script should be the first piece of code on the page to function correctly.

How to do 301 redirect in PHP ?
If you want to redirect a single page to another location then put the following code in the old page which have been moved.

<?php
 header('HTTP/1.1 301 Moved Permanently');
 header('Location: http://www.new-domain.com/new-file-name.php');
?>

If you want to redirect non-www to www domain name then put the following code into top of every page of the website

<?php
if (substr(getenv('HTTP_HOST'),0,3) != 'www')
{
  header('HTTP/1.1 301 Moved Permanently');
  header('Location:http://www.'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
}
?>

How to do 301 redirect in .htaccess file?
Put the following code into the .htaccess file and upload it into the root folder of the website. Note that this file only works for the website which is running under Apache Web Server.

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s