Wednesday, October 3, 2012

Apache: fall-back mechanism


This is the recent requirement that i had faced w.r.t. virtual hosts.

  1. at a given location see if the static resource exist
  2. if it exist then serve form that location
  3. otherwise fallback to a default location

after a day of investigation… this was the solution i finally discovered


NameVirtualHost *:81
#telling virtual host to listen on this port
<VirtualHost *:81>
ErrorLog logs/rewrite-log
CustomLog logs/rewrite-log combined
DocumentRoot E:/proj/nono2_2_10_3/config/
LogLevel debug

<IfModule rewrite_module>
RewriteLog logs/rewrite.log
RewriteLogLevel 3

RewriteEngine on
    #if the file at the below path doesnt exist then the rule applies. Both these are part of the same rule
RewriteCond %{DOCUMENT_ROOT}httpd/templates-mobile/js/$3 !-f
    #rule is to serve the files from default templates folder and skip the next rule (see s=1). Make this rule as the last rule(see L)
    #note the $1,$2,$3 are interpreted from he below rule. Each ‘()’ represents $n. where n increments per bracket
    # the above rewriteCond take $1,2,3 from this rule below
RewriteRule ^(/ui)(/tpl/js)(.+)$ %{DOCUMENT_ROOT}httpd/templates/js/$3 [L,S=1]
    #serve from templates-mobile folder     
RewriteRule ^(/ui)(/tpl/js)(.+)$ %{DOCUMENT_ROOT}httpd/templates-mobile/js/$3 [L]


Hope this helps somebody…