Monday, 17 March 2008


First thing to get your head around is the fact that it's kinda backwards. Messy URLs still exist, you're just translating them from nice to messy for your code to understand.

After that, it's quite simple really. Regex expressions: from, to. Use ^ and $ to mark the beginning and the end of the expression to match. Here is a good mod_rewrite cheat sheet although there's loads of help online. Try to set a RewriteBase to set the base directory within the site

the [R] flag will redirect the user and the URL in the browser window will change automatically. The [L] flag will redirect behind the scenes but the users won't know. Use this for query-string stuff. Remember to turn the engine on (as it's off by default) with RewriteEngine on.

The only other thing I've discovered so far is there seems to be a default index being made by Apache which I haven't been able to disable yet. This default indexing causes Apache to ignore my rules and display any file in a directory by default if they share the same name i.e. if I have rules like

RewriteRule ^$ catalog/ [R]
RewriteRule ^catalog/$ hello.php [L]

and within the directory there's also a file called catalog.php, Apache seems to be ignoring the redirect to hello.php and just displays catalog.php as the index. I know this because if I change the filename and the redirect directory to something else I have the same affect. I'm still working on this, no idea how to do it.

All of this can be written in a .htaccess file.

No comments: