There are many times when one may wish to redirect a visitor on his or her site. Say for example you used to have your blog a yourdomain.com/blog but now you want it in the root; you could redirect all traffic from the /blog directory back to the root / directory. Or, perhaps you’re moving to an entirely new domain; you could redirect all traffic from one domain to another. Like the first example, if you used to have webpages in a subfolder (like, /articles/article1.html) and you change your structure directory, you could redirect traffic from /articles to /entries.

One could come up with hundreds of reasons to redirect traffic and since there are so many reasons to redirect, there are a handful of ways to accomplish the redirection. The easiest and cleanest method is by editing your .htaccess file. The .ht (or, hypertext) access file is a directory-level configuration file that allows for “decentralized management of a web server1.” Incidentally, the file begins with a dot (.) because this is the default ‘hidden’ mode for *Nix servers. Anywho, the .htaccess file can do a lot of stuff and is very powerful, so always make a backup when editing the file and don’t erase things you find when you get there…always append, or prepend, and add a comment about what the code is meant for. (See #4 on Donnie’s list…in fact, read them all while you’re there.)

A few mote notes about .htaccess:

  • Always transfer your .htaccess file to the server is ASCII mode; binary will mess it up and could cause your server to operate incorrectly.
  • If you can’t see your .htaccess file, make sure your FTP server is set show hidden files. Also, as mentioned before, you won’t find this file on a Windows server…*Nix only. You can always try uploading abc.htacccess.txt and then renaming it once it’s on the server to simply “.htaccess” if you’re having trouble.

301 Redirect

To move a single page:

Redirect 301 /oldpage.html http://www.example.com/newpage.html

To move your whole site:

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

What if you’ve changed your site from .html extensions to .php or vice versa? The following will help you redirect one extension to another on the server-side; it looks for any .html extension and changes it to .php:

RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php

Other Methods

Remember at the beginning of the article when I said that editing the .htaccess file was the “clean & easy” way of redirecting? Well, not everyone has access to their .htaccess file so there is an alternate method. You can *gasp* use a meta redirect.

Search engines don’t like meta redirects and most people don’t either. They’re clumsy and they’re not bullet-proof like the .htaccess method. Either way, it can be used in a pinch. For this method, we’ll open the index.html or index.php file (or header file, if you’re using SSI) and add a few lines of code:

 

<meta http-equiv="refresh" content="10; url=http://example.com/">

The content=”10;  tells the browser how many seconds to wait before redirecting…you could easily change this to meet your needs.

Final Notes

All of my notes above have the intent of creating 301 redirects, as opposed to 302s. A 301 redirect is meant to be a permanant change. If Google and other spiders see a 301 redirect in place, they will begin updating their records accordingly.

Conversely, if you create a 302 redirect, you don’t lose your search engine friendliness. Here’s a helpful image to help you see the difference:

So, there ya go; a quick run down on redirecting visitors on a *nix box. Any questions?