301 redirect in PHP and .htaccess
Posted on December 29, 2007
Filed Under htaccess, php
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 keep loosing 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 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, its 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.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]
The significance of mcdba certification is acknowledged by the data base administrators. The administrators of networks may earn great prominence by getting network+ certification. There are different online institutes which offer their service for the exclusive mcsa training with %100 guarantee of success. The administrators and system analysts are provided with the special mcp training for becoming IT professionals. The security training helps the networking administrators and engineers to use all such protective measures against any hacking attacks. All kinds of information are provided for the individuals who want to get cisco training via online sources. For the preparation of different kinds of exams, microsoft training is provided for the candidates.
Popularity: 15% [?]
Follow me on twitter at http://twitter.com/roshanbh.
Related Posts
» How to redirect browser to https (ssl) in php
» 5 useful url rewriting examples using .htaccess
» Change default directory page using .htaccess
» Prevent Directory Listing using .htaccess
Comments
5 Responses to “301 redirect in PHP and .htaccess”
Leave a Reply






[...] If you type yahoo.com in browser it will be redirected to http://www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess. [...]
very helpful article..keep it up your work
Which brings up the question - is there a point in keeping your old page up if the .htaccess redirect is in place? In other words, does a search engine need both the page and the .htaccess redirect to complete the redirect, or only the .htaccess redirect?
No not at all…you don’t need to place the old page up for the .htaccess redirect
[...] If you type yahoo.com in browser it will be redirected to http://www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess. [...]