Dev Notes

Software Development Resources by David Egan.

Add a .htaccess File to a Jekyll Site


Apache, Jekyll, Performance
David Egan

Adding a .htaccess file to your document root allows fine control over access permissions.

Amongst other things, .htaccess rules can set:

  • In-browser caching
  • Access - you could allow/disallow access from certain IP addresses or ranges
  • Redirects
  • Rewrites

You can also prevent modification of code over 3G on some European providers (I’ve experienced UK providers in particular totally mangling site styles).

When setting up WordPress sites, we would typically lock down access to the entire admin area by IP address as a security measure. While this isn’t necessary for Jekyll sites (where there is no login), .htaccess rules can be a useful way of controlling how your site resources are cached.

A proper .htaccess thereby has the potential to speed up site loading times.

##.htaccess in Jekyll By default Jekyll excludes dotfiles - but you can easily override this behaviour:

In config.yml (or config_prod.yml if you have a production environment config file):

include: ['.htaccess']

Jekyll will now build the .htaccess file - which is much more convenient than editing the file on the server.

Add the .htaccess file to the root of your Jekyll project

Sample .htaccess for Cache Control

# Set browser caching
# ------------------------------------------------------------------------------
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
# End caching block

comments powered by Disqus