A cool tidbit for Apache...

I’m hacking around on the email-to-twitter thing a bit tonight (almost done with version one so watch for that announcement probably tomorrow)…anyway as part of it, I needed to do some Apache rewrite stuff…and I thought it was cool enough that I would just share it up here as tonight’s post.

Basically what I wanted to do was execute the code if the script exists…otherwise, I want to assume the user is attempting to access a message and is passing in a message key.

To accomplish this, I basically have to check if a file exists…if it does, just go to it, but if it doesn’t take whatever was passed and use it as the key for the viewmessage.php script.

Of course I had to have mod_rewrite installed and enabled for apache2 … but I’m not going to go into those details right now (do a google search for ‘mod_rewrite apache’ if you need help, the first few results will have all the gory details).

Anyway, here’s the rules I put in place (that seem to be working pretty good so far):

RewriteEngine On

# Is the request for a non-existent file?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-zA-Z0-9]+) viewmessage.php?id=$1 [NC]

This post has received 44 loves.


ARCHIVE OF POSTS



This is the personal blog of Kevin Marshall (a.k.a Falicon) where he often digs into side projects he's working on for digdownlabs.com and other random thoughts he's got on his mind.

Kevin has a day job as CTO of Veritonic and is spending nights & weekends hacking on Share Game Tape. You can also check out some of his open source code on GitHub or connect with him on Twitter @falicon or via email at kevin at falicon.com.

If you have comments, thoughts, or want to respond to something you see here I would encourage you to respond via a post on your own blog (and then let me know about the link via one of the routes mentioned above).