There are many reasons for redirecting a user to another page. Perhaps the page has moved or perhaps they are trying to access a page they shouldn’t have access to. Or maybe you want to put in an interstitial ad (those full page ads displayed before taking you to the actual page you want to go to).
There are also many ways of implementing a redirect, each with advantages and disadvantages appropriate for different scenarios.
HTML Redirect/META refresh
This is probably one of the most widely used since it is easy to implement. Using a <meta> tag, once a page is loaded, users can be redirected to another page after a certain amount of time. All those “you will be redirected in 5 seconds” pages use a <meta> refresh tag to accomplish this.
The code goes in the <head></head> section of your HTML and looks like the following:
<meta type=”http-equiv” content=”7;url=http://www.jonlee.ca” />
The number before the semi-colon indicates how many seconds the browser should wait until redirecting the user to the page specified. If an URI is not specified, the page will refresh itself after the set amount of seconds — useful for continuously updating a page without having to use JavaScript.
The PHP Redirect (and other languages)
Using a server-side language, you can invisibly redirect the user to another page. For example, this can be useful if they try to access a secure page without proper authorization and you wish to redirect them to a login page.
The syntax varies with language but here is the PHP code:
<?php
header(”Location: http://www.jonlee.ca/”);
?>
This modifies the header to send the user to a desired page. Note that headers can only be modified if no output has been sent yet.
.htaccess Redirect
Using .htaccess to redirect has several purposes. Similar to forcing www, you can use mod_rewrite to send users to a different destination. The code is simple — redirect all page requests to a new domain.
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
The advantage to this method is that it will maintain the page URI the user was trying to access. For example, if they navigate to http://www.olddomain.com/contact.php, they will be redirected to http://www.newdomain.com/contact.php instead of just the domain root.
Moved Permanently - 301 Redirect
Also, note the R=301 option at the end of the .htaccess code above. This 301 code is to let search engines know that the domain has been moved permanently and all old links should be updated to the new domain.
A 301 Redirect can also be achieved in PHP by adding this line prior to the redirection line:
header( “HTTP/1.1 301 Moved Permanently” );
If you’re into SEO (search engine optimization), listing the old domain as 301 is very important.
JavaScript?
There’s actually another method I left out and that is redirecting with JavaScript. I left it out because I can’t think of a situation where it would be better suited than the other methods described above. That, and there are more compatibility issues with JavaScript.
Know of any other ways of redirecting?
Popularity: 7% [?]
Tags: 301, How-to, htaccess, HTML, PHP, redirect, web development









Entries (RSS)