triptico.com

Un naufragio personal

Overriding mod_rewrite if a file exists

I use mod_rewrite intensively on this site, mainly to make the transition from the old static files layout to this new, dynamic one based on my Gruta CMS. Following the premise that URLs must be eternal, I've used mod_rewrite magic to make old URLs like

http://triptico.com/software/mp.html

still be accesible, but be interally redirected to

http://triptico.com/?t=STORY;topic=software;id=mp

Anyway, it may prove useful to only do this redirection if a file with that path and name does not already exist in the filesystem. This can be used, for example, to serve special, manually crafted HTML pages, statistics made by other software, or to 'freeze' a special page to avoid too much server load.

So these are the magic words:

 RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}      -f
 RewriteRule ^ %{DOCUMENT_ROOT}%{REQUEST_URI}    [L]

They must be entered before any other rewriting directives.

What they mean is, if a file exists (-f) with a path formed by the site's document root plus the requested uri, rewrite the query to it and stop rewriting ([L]).

Related

Visitor comments

James
2010-07-27
Fantastic info! I've been looking for this and almost lost hope that I will find it! Thank you! :)