w3schools Logo
Monday 21 May 2012
Www.w3schools.in Developer Network
HTML tutorials
CSS tutorials
php tutorials and scripts
MySql tutorials
Learn C Programming
Operating system

301 redirect in php and .htaccess
From w3schools.in

What is 301 redirect ?

You have a website and its all pages are ranked in the search engine. And now you’ve moved these files to a new domain. What will happen in this scenario! You are sending the visitor to “Error 404 – File not found” page when they follow your website in the search engine. Furthermore, if you place the custom error page, then also you will be keeping losing ranking in upcoming days in search engines as the original file could not be found in the old URL. So what is the solution for this?? The solution is 301 redirect which means moved permanently. It is the most efficient and search engine as well as visitor friendly method for the webpage redirection.

Why do you need 301 redirect?

Have you ever typed yahoo.com in your browser, what happens next is it will redirect you to www.yahoo.com; it’s all the result of the 301 redirect. So, why do you need to do so? If you’re not doing so, then you’re splitting yourself to two different URL and search engine spiders can also crawls your site’s content by two different URL which may result to “duplicate content” penalty by search engines if search engines collect two copies of the same data from two different URLs.

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.domain.com/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]

This page was last modified on 10 Sep 2010 at 22:38:52.
Gautam Kumar
EDP Manager
http://www.connectsin.com
Related Posts
» Useful functions to tighten the security in PHP
» CodeIgniter A recommendation for PHP Programmer
» PHP Framworks Why when and which
» Solving Floating point number precision lost problem in PHP
» Handling array of HTML Form Elements in JavaScript and PHP
» How to filter user submitted data easily in PHP
» Prevent form post request from another domain in PHP
» Flaw in and or logical operator php
» Default arguments functions php
» Submit Form without Refreshing Page with Jquery
» MySQL Performance Tips
» Free ajax chat applications in PHP
» Return More Than One Value From a Function in PHP
» PHP Optimization Tips
» PHP Error Handling
» Useful PHP Classes and Libraries
» Useful Tools For PHP Developers
» PHP Frameworks
» Web based HTML Editors
» 301 redirect in PHP and .htaccess
» Hiding PHP file extension
» Rredirect Browser HTTPS SSL PHP
» Tighten php security functions
» PHP Framworks