Forum Moderators: phranque

Message Too Old, No Replies

Conditional .htaccess mod rewrite rules

if... then... else possible?

         

trillianjedi

12:53 pm on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had this thought today (having one site using a rather old MySQL based CMS system) - if I mod_rewrite my urls like so (this is how links appear on my pages):-

URL : www.example.com/review_10.html

... gets internally re-written (so the CMS can understand it) to :-

www.example.com?review=10&view=full

Where that example rarely (if ever) changes in content or markup, I could dump it to disk via some kind of shell script calling wget:-

wget www.example.com/review_10.html

That would then dump a static version of the page on disk, to save DB and PHP having to be involved in future viewing of that page.

To do that, I need to do something conditional like this in .htaccess:-

if (!file_exists(url)) {
do mod_rewrite rule
}
else {
do nothing, let apache serve as static file
}

Essentially, it's a form of caching - try to dump static pages, under the "new" URL to disk and serve those where they exist, otherwise do the mod_rewrite rule to let PHP generate the script dynamically.

I hope I'm making sense here. I'm curious if this is possible, and if so is anyone doing anything like this and if so, does it work and does it work well or are there considerations which mean it's a bad idea?

TJ

jdMorgan

5:30 pm on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the RewriteCond directive, specifically,

RewriteCond %{REQUEST_FILENAME} -f

or alternatively

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f

That, combined with the [L] flag on RewriteRule, will allow you to create an "If file exists then x, else y" construct.

Jim

trillianjedi

9:08 pm on Aug 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

Brilliant - thanks. That's a very very powerful thing.

For anyone with an old-skool DB dependant CMS, it'll save an awfull lot of resources.

TJ