Six Ways of Redirecting Visitors to a New Page

AddThis Social Bookmark Button

PHP, JavaScript, META and Htaccess Redirection

Do you need to redirect your users to another page? There are a six different ways you can get it done, two methods using PHP, two methods using JavaScript, and one way using META Refresh or via htaccess file. There are different reasons for using different methods. Here is the code with some explanations:

1. Direct PHP Redirect: This is a direct redirection using server side PHP code without any delay. No client side code is processed (no infomation is sent to the browser), the user is immediately taken to the page specified in the PHP file.

<?php
header("Location: http://www.example.com/"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;
?>

2. PHP Redirection with Delay: This is a PHP redirect with a specified delay in seconds. During this delay, PHP code can be processed and passed to the browser. Using the code below, the user will see the message “You will be re-directed in 5 seconds…” for 5 seconds before the browser redirects user to the page specified.

<?php
header( "refresh: 5; url=http://www.example.com/" );
echo "<h1>You will be re-directed in 5 seconds…</h1>";
?>

3. JavaScript Direct Redirection: If you don’t know PHP you can use JavaScript to redirect. JavaScript is done on client side so it even though this is a direct redirection, it can still send information to the browser. This means that you can process some javascript and then redirect the user, or redirect the user based on some process; in the first example of PHP direct redirection you cannot process anything on the client side.

<SCRIPT LANGUAGE="JavaScript">
window.location = "http://www.google.com/"
</SCRIPT>

4. JavaScript Redirection with Delay: This is almost the same as the direct javascript redirection shown above, however, here you can specify a delay time similar to the delay time specified in the second PHP example. 5000 means 5 seconds.

<SCRIPT LANGUAGE="JavaScript">
setTimeout("location.href=’http://www.google.com/’", 5000);
</SCRIPT>

5. Meta Refresh Redirection: This redirection method uses a HTML meta element to tell the web browser to automatically redirect the user to another web page after some time. According to W3C, it is bad practive to use META to redirect becasue it does not give any info about either the original or new resource to the browser or search engine. The “back” button will also not work in some browsers. But, it’s still another way to get the job done.

<meta http-equiv="refresh" content="5;url=http://www.google.com/" />

6. Htaccess Direct Redirectionn: My favorite way of redirecting is by using the .htaccess file. It has no delay because it is done server side and visitors get sent directly to the new page before the original page is server to the browser. Here are some ways of redirecting using the htaccess file (301 means Moved Permanently):

To Move a single page:

Redirect 301 /old-web-page.html http://www.example.com/new-web-page.html

To Move an entire site:

Redirect 301 / http://www.example.com/

Redirect www to non www version of site:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com/$1 [R=301,L]

Redirect non-www to www:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yoursite.com [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

Popularity: 6%


Tags:

,

,


If you enjoyed this post, please consider to leave a comment.

Comments

Beware of cutting and pasting out of this page. The author has used unicode symbols in the code instead of ascii characters. This means, for instance, that the ” character is actually &#8221. Your php engine will see the wrong character, even though IE displays it exactly the same. Luckily my Kate editor displayed it differently, so I was able to work out what was going wrong.

Bob Leslie: Thanks for catching that! I have updated this post so now everyone can just copy and paste!

Leave a comment

(required)

(required)