Apache

ID #357

How do I redirect traffic to another web page?

Applies to: Grid System

There are multitudes of ways to redirect web traffic. Here are four ways that work well:

  • HTML Meta Refresh:

    This technique will work in any HTML, and causes the browser to instantly load the site specified after url=. To introduce a delay, increase the number after content=; Typing a 5 would mean 5 seconds before redirection.
  • Apache Redirect:
    You can type the following into a .htaccess file to send anything matching the first URI value to the second URL:
    Redirect /path-to-redirect http://modwest.com/
    This would reroute, for example, http://yoursite.com/path-to-redirect/index.php to http://modwest.com
  • PHP HTTP Header Redirect
    In any PHP script, before actual HTML output has started, you can use the header() function to insert a location header, instantly redirecting the browser:
    header('Location: http://modwest.com/')
  • Apache mod_rewrite Redirect
    This is the most difficult but powerful of these four methods. In the example below, with code living in a .htaccess file, any traffic will redirect to modwest.com. Your imagination and the documentation above can produce other endless possibilities.
    RewriteEngine On
    RewriteRule .* http://modwest.com [R]

Last update: 2011-06-27 16:39
Author: FAQ Admin
Revision: 1.3

Digg it! Share on Facebook Print this record Send FAQ to a friend Show this as PDF file
Please rate this FAQ:

Average rating: 2.6 (5 Votes)

completely useless 1 2 3 4 5 most valuable

You can comment this FAQ

Comment of Anonymous:
For search engine friendly 301 redirects where you want to redirect a previous website or a non-www site to a www site (in order to avoid search engines from ranking them as two duplicate sites), see the following webpage: http://www.webconfs.com/how-to-redirect-a-webpage.php
Added at: 2008-06-02 10:07

Comment of Anonymous:
RewriteCond %{HTTP_HOST} !^www\..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*)\.(com|co\.uk)
RewriteRule ^.*$ http://www.%1.%2%{REQUEST_URI} [R=permanent,L]

This will rewrite all addon domains on a hosting account to www.domain.com versions if it's in the root folder.
Added at: 2009-01-23 08:29