mod_rewrite recipes I've used
mod_rewrite is a fun tool, you should read the Apache module mod_rewrite documentation for all the info. But to avoid doing that over and over, I'll details some mod_rewrite examples that I've come to use:
RewriteEngine On
RewriteRule ^site\-one\.css$ http://www.craimer.org/~shalom/site-one.css.php [P]
RewriteRule ^fds/ http://www.craimer.org/~shalom/offline/ [R=301, L]
Lines:
- turn on mod_rewrite
- Make any reference to "site-one.css" in the current directory, get filled by the URL to a PHP-generated CSS file. The "[P]" means "proxy", thus the data is fetched from http://www.craimer.org/~shalom/site-one.css.php by HTTPd itself, and the client isn't even aware the "site-one.css" isn't there.
- Anyone trying to access a file starting with "fds/" from the current directory (in other words, anyone trying to access the "fds" subdirectory) will get redirect to http://www.craimer.org/~shalom/offline/ with a redirect response code of 301, which means "moved permanently" (the alternative would be 302, which is "moved temporarily" which would have search-engines coming back to re-check).
The "L" in the brackets means that this is the "last rule" if matched. Like a "break" in C, if this rule is matched it won't continue to look for matching rewrite rules.
Last updated on 2007-07-16 03:51:14 -0700, by Shalom Craimer
Back to Tech Journal