However, I ran into a few snags when implementing it with rails 1.2.6. The pages were getting generated properly in the public/cache folder, but when I went to the page in question, the cached file wasn't being served. How cached files are served to a visitor all depends on the webserver that's running. In my case, I'm running rails on Apache, via fastcgi or cgi. This means that there's a file in the public folder, .htaccess, that controls how requests are sent to rails. This is an override file for apache, that lets you customize Apache settings and configuration for a particular folder. There's a section in the .htaccess file that tells apache to look for cached files first, and if none are found, send the HTTP request to rails, through dispatch.fcgi. Here's what that section looks like normally:
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Now since I'm placing all cached content in the cache folder, the section should be changed to this, right?
RewriteRule ^$ cache/index.html [QSA]
RewriteRule ^([^.]+)$ cache/$1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Wrong! This didn't work for me at all, I even tried using a leading slash, ie: /cache/. Apache refused to look in the cache folder, and cached files still weren't served. After much searching, I finally stumbled upon the correct apache directives for a custom cache directory. Here's what it looks like:
# This directive will look in public/ for cached files
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
# This directive will look in public/cache/ for cached files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^cache/(.*) - [C]
RewriteRule ^(.*)$ cache/$1 [QSA]
# If nothing is found, send to rails
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Finally, it worked properly. This particular set of directives also will look in just the public directory, so remove the first 3 lines(including the comment) if you don't want this to happen. It took me a while to find this fix, so I thought I would share.




