Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My Question is similar (almost the same) to another question asked here: but with a twist.

Does anybody have some working Apache mod_rewrite rules that enable Phusion Passenger (mod_rails) to use a non-default location for the page cache within a Rails application? I'd like the cached files to go in /public/cache rather than the default of /public.

In my case I have 2 custom directories. "/public/" and "/public/" for mobile requests.

This particular mod_rewrite code works for me ONLY for requests:

RewriteRule ^$ / [QSA]
RewriteRule ^([^.]+)$ /$1.html [QSA]

Now is there a way to take the or from the incoming url and substitute it for the directory location to look for the cached page? Everything about the url for both mobile and www requests are the same except for the prefix of the host "m" for mobile browsers and "www" for everything else.

Just to clarify I have mod_rewrite conditions/rules to detect mobile browser which works fine I just need to tell apache which cache directory it is located in based on the subdomain of the request.

share|improve this question

RewriteCond can look at the host header that the request came in with.

Something like this:

RewriteCond %{HTTP_HOST} ^m [NC]
RewriteRule ^$ / [QSA]
RewriteCond %{HTTP_HOST} ^m [NC]
RewriteRule ^([^\.]+)$ /$1.html [QSA]

RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^$ / [QSA]
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^([^\.]+)$ /$1.html [QSA]

But, since this is handling the different sites out of completely different directories, why not just do two different VirtualHost configurations for the hosts, with different DocumentRoot settings?

share|improve this answer
I tried the code and it worked but only if there was page.html in the cache directory otherwise it would give me a 404 error when it should hit the app to generate the page on the request if the page.html didn't exist. I think the VirtualHost idea is interesting but I want to serve every request from the same app – Ranknoodle Nov 16 '11 at 22:26
Well, sounds the mod_rewrite's doing all that it needs to do - is the request to the cache location being properly handled by the dynamic content handler? Or do we need to have it check if the file exists, and not do the rewrite in that case? – Shane Madden Nov 16 '11 at 22:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.