URL Redirects

I’m losing count of the number of ways URL redirection can be accomplished, but I thought I’d share my experience related to one method in particular:  Apaches’ rewrite_mod: (enabled using the command: a2enmod rewrite)

Below are two example rewrite rules I use to perform the redirects used on this site.  The rules utilize regular expressions  and are both derived from the apache2 documentation pages

#This rule sends all urls that don’t specify a file name (i.e. end in “/”) to this blog located in the blog directory
RewriteRule ^/$ /blog/index.php [R]

#This rule redirects all URLs ending in /Wiki or /wiki (NC=case insensitive) to the NotesWiki directory
RewriteRule Wiki$ /NotesWiki/index.php [R,NC]

Both of the above rules are placed in our site’s configuration file (e.g. ../apache2/etc/sites-available/”your site’s name” on a linux machine).  After you restart apache (i.e. /etc/init.d/apache2 restart on a linux machine) all uri’s pointing to www.medengineers.com redirect to www.medengineers.com/blog and  www.medengineers.com/wiki redirect to www.medengineers.com/NotesWiki.

You can also accomplish redirects by including custom .htaccess files in each directory you want to redirect, but most Apache aficionados frown on this practice b/c it adds an un-needed layer of complexity for the next Apache admin (i.e. it is usually better to have your configuration centralized, or else you end up spending hours chasing down all the .htaccess files).  However, if you don’t have full directory access, this is one of your only options (The other option is to use a java script redirect html page)

Hopefully, this saves someone some time.


This entry was posted in Apache, Programming. Bookmark the permalink.

One Response to URL Redirects

  1. bet365 says:

    how are you!This was a really splendid post!
    I come from uk, I was luck to search your Topics in yahoo
    Also I obtain a lot in your blog really thank your very much i will come again

Leave a Reply

Your email address will not be published. Required fields are marked *