June 18, 2007

Getting a RewriteCond to work

Wondering how to get a nice url such as

http://www.farmsoftstudios.com/tools/ajax/tutorials.html

to run a query for you? Here’s how to get this url: http://www.farmsoftstudios.com/tools.php?type=ajax&style=tutorials to the url listed above. You’ll need a .htaccess file in the directory that you are working from. Create that, and then add these lines:

RewriteEngine On
RewriteBase /your/sub-directories/if/any
Options +FollowSymLinks
RewriteRule tools/(.*)/(.*)\.html$ tools.php?type=$1&style=$2

To dissect that, first, you are calling an apache directive called RewriteRule. That tells apache that you want this url rewritten. Then you list the structure that you want created. In our case, that is tools/ (which is static) and then the query’s “variables.” What I mean by that is when I type

http://www.farmsoftstudios.com/tools/ajax/examples.html

into the address bar, it creates a new query with &style=example instead of tutorials. Pretty slick, eh?