Everything In Between

The brutally honest, first-person account of Meitar Moscovitz's life.

Fun with Apache Redirects

leave a comment

I absolutely love Apache. A while ago I ranted about the annoying prevalence of uneccessary redirects some sites make you click through. This topic comes up pretty often even today, although the issue of annoying HTTP “404 Not Found” errors has been mitigated somewhat by people wising up and creating helpful error pages. (Which I’ve been meaning to revise and improve on this site.)

Usually, the existene of broken links and the like is brushed off by saying something like, “We are undergoing a re-organization period,” usually followed by a request to “please bear with us” for the duration of said period.

Well, I know a thing or two about organization. In fact, I probably know more than most because I re-organize my my desk, my computer, my Web site, and even my refridgerator fairly frequently. As Danica will no doubt tell you, I like to make sure I am keeping things as effecient as possible.

In terms of serving up web pages to visitors, it means that I can reorganize and restructure my site as much as I’d like and you wouldn’t even know that I did it. The main tools I use for this are Apache’s Redirect directive (implemented via mod_alias) and the various, insanely powerful features offered by mod_rewrite (documentation). I’ve used these tools several times while working on this site but none of these examples even scratch the surface of what is possible.

  • While moving the site from Blogger to WordPress, I also decided to change the blog’s address so that I had a shorter URL, devoid of any hint of the old site. Naturally, since I didn’t want to break incoming links, I used a Redirect to bounce visitors to the right address. Additionally, I tagged the redirect with the keyword permanent which emits a “301 Moved Permanently” HTTP response header so that decent clients will no longer request the old resource’s address. The full line in my .htaccess file looks like this

    Redirect permanent /bpd/blog/ http://www.maymay.net/blog/

    although I could have written it with the status code instead of the keyword like this:

    Redirect 301 /bpd/blog/ http://www.maymay.net/blog/
  • At the same time, I also decided to give some common aliases to the /bpd subdirectory, so I started redirecting requests for things like /bipolardisorder to /bpd as well.

  • I wanted to make a few “static pages” such as the About Meitar page, but I wanted a cruft-free URL for them as well. The simple solution was to start Apache’s mod_rewrite and map a nice-looking URL to the file I wanted to serve.

    # turn mod_rewrite on
    RewriteEngine on
    # restrict the following rule to /bpd/
    RewriteBase /bpd/
    # set /meitar or /meitar/ to meitar.php (or similar)
    RewriteRule ^/meitar/?$ meitar.php
    

Most recently, I was asked if there is some way Apache could automatically “redirect HTTP to HTTPS” transparently. Sure enough, this is a piece of cake:

RewriteEngine on
# is request not on HTTPS?
RewriteCond %{HTTPS} !=on
# if so, redirect it (the [R] flag) and stop processing (the [L] flag)
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [R,L]

Written by Meitar

December 13th, 2004 at 10:01 am

Posted in Unix/Linux,Usability

Leave a Reply