2 June 2010

Using Rails to create a static site

I have a Rails-driven website which changes infrequently and contains mostly static content. It is cheaper and almost as convenient to run it as a static site than to run it as a Rails app.

A Google search threw up Using Rails to Create a Static Site, which describes a method for doing what I want by tweaking the Rails app and then using wget to generate the site. I used a slightly different method to the one described.

Step 1: change the routing

Add the .html extension to all routes. For example, change the default route from:

map.connect ':controller/:action/:id'


map.connect ':controller/:action/:id.html'

All URLs and internal links will now have the .html extension and the site will look to users like a static site.

Step 2: Disable asset timestamps

These are very useful for ensuring that assets that have changed since they were last requested are not served from the browser cache, but for a largely static site are really not necessary.

In config/initializers/ (or in older versions of Rails, inside config/environment.rb) add the following:


Step 3: Use Wget to download a copy of the site.

Start the app on your local machine (assuming port 3000) and run wget:

wget -m localhost:3000

The static site with all links preserved will be sitting in the folder localhost:3000.

Step 4: Zip up the site.

cd into the new folder and run tar:

tar -cf site.tar *

Step 5: Copy the site to its new location

FTP the tar file to its new location (and make sure it is at the document root or you may have problems with the link paths). Unzip it:

tar -xf site.tar

And delete the tar file.

Job done.

d. sofer