Pretty URLs

My new blog software generates pretty URLs using a technique adapted from an article at A List Apart. In a nutshell, all requests for PHP files are redirected back to index.php using mod_rewrite commands in the .htaccess file. If the URL points to an actual file, the file is included. If no such file exists, the directories in the URL are converted into variables and run through select statements. Overall, I’m pretty happy with how it works. However, I’ve run across two problems:

  1. The article says one of the conditions for deciding whether or not the URL points to a file should be:

    file_exists($DOCUMENT_ROOT . $REQUEST_URI)

    If you don’t use query strings anywhere, that would be fine. But I like to use query strings for my non-public administration files. It’s just easier that way. To scratch my particular itch, I changed the condition to:

    file_exists($DOCUMENT_ROOT . ereg_replace("\?.*$", "", $REQUEST_URI))

    The next line should then be changed to:

    include($DOCUMENT_ROOT . ereg_replace("\?.*$", "", $REQUEST_URI));

  2. .htaccess and the mod_rewrite commands in it are being applied to all directories below the document root. To exempt a directory from being affected by mod_rewrite, I have to add this to .htaccess:

    RewriteCond %{REQUEST_URI} !^/directoryname/*

    This gets messy as I add more directories. What’s the correct solution? I’m sure it’s totally obvious, but I’m rather new to using .htaccess.

Posted in Uncategorized

One Response to Pretty URLs

  1. Matthew says:

    The RewriteCond above is just silly. RewriteCond %{REQUEST_URI} ^/[0-9]{4}/ works much better. It allows me to use any name for a directory as long as it isn’t four digits long.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>