si-blog

Help me out with mod_rewrite

Posted May 30, 2004 in si-blog.

I think I am beginning to understand what might be involved when it comes to making nicer URLs. I don't know much about mod_rewrite, nor do I know much about regular expressions. I think I've come up with a way to do it, but I need someone to confirm or correct.

Preferred URL
/archives/2004/05/30/some-post-title
Actual URL
/archives/index.php?date=2004-05-30&title=some-post-title
mod_rewrite rule
RewriteRule /archives/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9\-]+) (line break here)
/archives/index.php?date=$1-$2-$3&title=$4

How am I doing?

Comments

  1. Gravatar

    Interesting, I'd like to see where this goes. I simply break off at /weblog/ and pass the whole remaining string to my script, where PHP deals with it. So the RewriteRule would be

    ^/blog/(.*)$ weblog_script.php?$1

    So for me, PHP would get this:
    2004/05/30/some-post-title

    as the $_SERVER['QUERY_STRING'] value. From there I explode the value based on /, and serve up the page accordingly...

    Posted by Mike P. on May 30, 2004.

  2. Gravatar

    I currently use:

    RewriteRule ^archives/([0-9]{4})/([0-9]{1,2})/([0-9a-z-]+)?/?([0-9]+)?/?$ /archives-year-month-post.php?year=$1&monthnum=$2&name=$3&page=$4 [QSA]
    RewriteRule ^archives/([0-9]{4})/([0-9]{1,2})?/?$ /archives-year-month.php?year=$1&monthnum=$2 [QSA]
    RewriteRule ^archives/([0-9]{4})/?$ /archives-year.php?year=$1 [QSA]
    RewriteRule ^archives/?$ /archives-file.php [QSA]

    Which allows me to use multiple templates in WordPress (I will write something about that hopefully). I think the '-' in your rewrite rule kill the process here, you might try it without them so you cat a timestamp instead of a formatted date.

    (Of course, I use more rules for other stuff as well, but this is probably most relevant at this part of the process.)

    Posted by Anne on May 30, 2004.

  3. Gravatar

    So my attempt won't work then? I thought that the hyphen would be okay as long as it was escaped?

    Posted by Simon Jessey on May 30, 2004.

  4. Gravatar

    Where do you have it escaped then? Not sure if that will work by the way. You should try something that does work and base you system on that in my opinion ;-)

    Please add the LABEL element to your comment form by the way.

    Posted by Anne on May 31, 2004.

  5. Gravatar

    It would be interesting to hear what people say about this, and I in particular would like to hear about the difference between my way and your/Anne's way, for example.

    Our blog went the way it did for simplicities sake.

    Maybe it's just me (i.e. correct me if I'm wrong), but a lot of the rewrite rules that I see in use, those similar to the one's outlined above, are being used because the url's are being corrected to fit a system (like getting user-friendly urls working with Wordpress, for example).

    The way we approached it was to build the system and the urls together. As a bonus, I have found this particularily handy for building custom error messages.

    Posted by Mike P. on May 31, 2004.