If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!
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.programimi.com” />
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.programimi.com/”);
?>
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?
02 Apr
Posted by ProCOM
on April 2, 2008 – 1:25 am - 512 views
One of the first things I do when I begin implementing a site is to force the www (or no-www) in the domain name. Why would I want to do this? For search engine optimization of course. For example, programimi.com redirects to www.programimi.com. What this means for Google is that it gets crawled as one site. Sites that link to programimi.com will add to the pagerank of www.programimi.com. Theoretically, this could double your Pagerank score (but not necessarily your Pagerank rating).
Anyway, here is the code to add to your .htaccess file to force www, but make sure mod_rewrite apache module is turned on:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(.*)\.programimi\.com$ [NC]
RewriteRule ^(.*)$ http://www.programimi.com/$1 [R=301,L]
And here is the code to force no-www (in spirit of no-www.org):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.programimi\.com$ [NC]
RewriteRule ^(.*)$ http://programimi.com/$1 [R=301,L]
Of course, replace programimi.com above with your own domain name. To edit your .htaccess file you may need to enable view hidden files, or if the file doesn’t exist, you’ll have to create it yourself. The best way is to do it through an SSH connection to your server.
If you don’t have access to .htaccess, you could also accomplish the same thing through php with a simple script at the top of every page that will try to find www in the URL and redirect if found. Not the most elegant solution, but it does what it needs to do for those who can’t edit .htaccess for some reason.
if(!substr($_SERVER[’HTTP_HOST’], ‘www’)) //check if www exists in URL
{ header(’Location: http://www.programimi.com/’); //redirect to www.programimi.com
}
Or for those using wordpress, you could simply install a plugin that does all this for you! One example plugin is WWW-Redirect.