How to Write a 301 or 302 Website Redirect Using Php

The use of 301 redirects and 302 redirects is a convenient method of sending traffic from one web address to another, without passing the surfer through an intermediate page. These redirects are particularly useful if your web site uses multiple domain names, either for marketing purposes or to capture common misspellings. They are also useful if your website uses multiple servers and one will be down for scheduled maintenance. The primary difference between the two is that a 301 redirect tells the web browser that the address has changed permanently, while a 302 redirect is used for temporary changes, in which the original address may become functional again at some point. If you would like to redirect traffic on your website, here is how to do it using PHP to initiate a 301 or 302 redirect.

Things You'll Need

  • Computer Connected to the Internet
  • Windows Notepad or HTML Editor
  • FTP Program
  • Access to the Web Server
Show More

Instructions

  1. Write a 301 or 302 Redirect for a Specific Page Using PHP

    • 1

      Open your html editor, the same as you would use to create any html or php file. If you do not have an html editor, you can use Windows Notepad instead. From your Windows PC, click Start>All Programs>Accessories>Notepad.

    • 2

      For a 302 redirect, copy the following text into a new document: <?php header(”Location: http://www.domain.com/temporary-address/temporary-file-name.html”); exit(); ?> Note that you should change the url in this example to be the url of the page you are redirecting visitors to temporarily. For a 301 redirect, copy this text into the document instead: <? Header( "HTTP/1.1 301 Moved Permanently" ); Header( "Location: http://www.new-url.com" ); ?> Again, make sure to change the url to the address of the page you are redirecting your traffic to.

    • 3

      Save the file. Click File>Save As, and then save the file with the same name as the page that you will be redirecting traffic from.

    • 4

      Use your FTP software to upload the file to the server. If you do not have an FTP program, you can download either Smart FTP or Filezilla free of charge. Many web hosts will also provide a web-based file manager for uploading files. Be sure to “overwrite” or “replace” the original file when prompted. After the file has been uploaded, anyone who types the url to the original file will be transferred to the new.

    Write a 301 Redirect for an Entire Website With Htaccess

    • 5

      Open your website’s existing “.htaccess” file in your standard html editor. If you do not have an html editor, you can use notepad, as instructed in the previous sections. If your web site does not already have a “.htaccess” file, that is ok, you can create a new one.

    • 6

      Copy the following text into the htaccess file: Options +FollowSymLinks RewriteEngine on RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L] Note that you should change the url in this example to match the url of the site you are redirecting your traffic to.

    • 7

      Save the file as “.htaccess” (yes, the dot comes first) with the file type of “All Files” and upload the file to the root directory of your website. Now any time someone goes to an address on the old site, they will be taken to a corresponding address on the new site. For example, someone going to “oldsite.com/abc123.html” will now be directed to “newsite.com/abc123.html”. This is very useful if you want to change domain names without losing all of the links and bookmarks that point to your website.

Tips & Warnings

  • It is generally not recommended to use a 302 redirect unless you are a search engine expert and can make a unique redirect page for each search engine. Each of the three major search engines (Google, Yahoo and MSN) process 302 redirects in a different manner.

Related Searches:

Comments

  • Anes PA Dec 07, 2010
    Nice Stuff, Well done bye Anes

You May Also Like

Related Ads

Featured