Heal Your Church WebSite


Teaching, rebuking, correcting & training in righteous web design.

mod_rewrite madness part one

As you recall from my “proof of concept” articles, I’m thinking of tweaking either MovableType or pMachine so I can use it as one would use a content management system. This of course raises several issues, not the least of which are the long-winded URLs (Uniform Resource Locator).

For instance, take some of the URLs on this site, that use numbers to refer to an article.
http://www.healyourchurchwebsite.com/archives/000432.shtml

Sure, digits are okay for computers, but we people like to remember stuff by name. For example, referring to an article by its title:
http://www.healyourchurchwebsite.com/Proof of Concept

It doesn’t take a rocket scientist to figure out that the later is much easier to remember than the former. Moreover, it is very likely search engine are going to list, index and rank the link with a title over one with a ubiquitous numeric. That said, I’m sure some of you are wondering, “yeah, slick Dean, except I really don’t want to have to hack MT or pMachine so it saves by Title.” Wll, I hear you and that’s NOT what I’m suggesting.

Rather than modify a moving target, that is our blogging software, why not take advantage of mod_rewrite. In as plain English as possible, mod_rewrite is a tool to rewrite incoming URL requests. That is, when your browser comes to my church web site, I’m going to employ mod_rewrite to see if you’re requesting an article by title, and if so, go into the database, find the corresponding number and deliver you the post you’re looking for. This should work especially well for my church website as blog titles will be short and sweet, such as “About Us”, “Meet the Staff”.

The question is, how do we skin this cat? The mod_rewrite engine is a rather powerful yet complex engine. The syntax is even difficult for code monkies such as myself. Well, after reading an article on How to Succeed With URLs over at A List Apart, I settled on modifying my .htaccess file so it would direct input for everything but images and style sheets into a routing program – index.php

I decided this approach would be better as I was having some problems with some of the more complex mod_rewrite directives because my Host Provider has various features turned off for security purposes. Fair enough. Moreover, by depending on a second script file, I can keep the .htaccess file simple, and add comlexity using a scripting language that is a bit more flexible and easier to understand and implement. Here’s what I did in a sub directory I’ll call “mytestdir”:

RewriteEngine On

# do not process image files and style sheets
RewriteCond     %{REQUEST_URI}  \.(gif|jpg|png|css)$
RewriteRule     (.*)            -               [last]

# pass index.php staight up
RewriteCond     %{REQUEST_URI}  ^/mytestdir/index.php(.*)$
RewriteRule     (.*)            -               [last]

# suck down the rest as an arg
RewriteCond     %{REQUEST_URI}  ^/mytestdir/(.*)$
RewriteRule     (.*)    index.php?a=$1

I’d like to hear from some of you. It works for me, but I’m more than willing to admit I may have made some goofs on this one. So pitch in if you’ve got something better. In the meantime, here are some other resources you might find helpful on the topic:


p.s. many thanks to the following at 196.12.38.204 for their comment spam:
hari@stph.net and
info@partypoker.com

8 Comments

  1. Dean, As a matter of interest what do you see as the best church websites online at the moment? My personal favourites would probably be http://www.hillsong.com http://www.millenniumclc.com and http://www.chc.org.sg I was just wondering what you thought, or if there are any really good ones around!

  2. I know what you mean about URLs — they are a pet peeve for me as well. I have some links over at my blog (www.deanebarker.net) on readable URLs, including a recent article and a link to something Jakob Nielsen wrote on the subject a few years back.

    I have never used mod_rewrite, but for the Minnesota Vikings web site, we used an ISAPI filter (IIS — ugh) to pull dynamic pages from a nice, friendly URL.

    Deane

  3. What happens when you have two articles with the same name?

    I’ve solved this on my site by including the month and year in each URL. So a recent entry entitled “Java Spyware” has a url of http://kalsey.com/blog/2002/11/java_spyware.stm

    That way, I only need to worry about repeating titles within the same month. If I wanted, I could even change the format to /2002/11/06/java_spyware.stm

    Movable Type supports this natively. No need to hack. Use this as your Individual Archive filename:

    <$MTArchiveDate format=”%Y/%m”$>/<$MTEntryTitle dirify=”1″$>.html

  4. Pingback: WOIFM :: An Institute of Higher Learning