301 redirect in PHP and .htaccess

Advertisement

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: 6% [?]

Tags: ,

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

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

18 Comments on “301 redirect in PHP and .htaccess”

  • php-web-developer wrote on 20 May, 2008, 7:21

    very helpful article..keep it up your work

  • Domain Redirects wrote on 24 June, 2008, 17:42

    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?

  • Roshan wrote on 24 June, 2008, 18:39

    No not at all…you don’t need to place the old page up for the .htaccess redirect

  • Science & Technology wrote on 6 May, 2009, 18:30

    I made a .htaccess for mod_rewrite from non www to www site….
    It worked fine except that I could not log on to the admin page of the site and had to remove the file….any additional suggestions ??

  • George Garchagudashvili wrote on 17 September, 2009, 15:29

    very nice tutorial

  • M K Rana wrote on 5 December, 2009, 13:15

    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

  • sreenath wrote on 3 May, 2010, 10:07

    when i rewrite
    RewriteRule ^tourpackages-([0-9]+)\.html$ tourpackages.php?category_id=$1
    i cannot access the category_id value from tourpackages.php by
    $category_id=$_REQUEST['category_id'];
    can you please help me to solve this

  • Arjan wrote on 28 June, 2011, 13:14

    What a great site this is! These information about htaccess helped me a lot with making the proper urls. Thank you very much!

Trackbacks

  1. 5 useful url rewriting examples using .htaccess
  2. Multibytes India » url rewriting
  3. 5 useful url rewriting examples using .htaccess « Captain Tsubasa Logs…
  4. GMeetSoft » .htaccess Magical Rules
  5. Url Rewriting using .htaccess | Online Auction Owners
  6. .htaccess URL Rewriting Examples - Abdullah Yousafzai's Web Log
  7. 5 useful url rewriting examples using .htaccess | Sharjng Center | Technical Sharing
  8. Sidambara Selvan Blog » Blog Archive » Useful URL rewriting examples using .htaccess
  9. L’URL Rewriting en pratique avec 5 exemples - Cyril
  10. 5 useful url rewriting examples using .htaccess « Irf@n'$ blog

Write a Comment

 


Copyright © 2014 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.