Cache a website with an .htaccess file

 

Overview

This article shows you how to use various tools in an .htaccess file to optimize your website's performance.

See this article to learn more about how to create an .htaccess file on your web server

If the file already exists, see the following articles for instructions on how to update it (depending on if you're using an FTP client or SSH):

Caching tools

You can use tools such as mod_deflate and mod_expires to compress or cache various types of files.

The following examples show default settings on DreamHost servers and do not need to be enabled. However, you can adjust these values in your .htaccess file as needed.

GZIP

DreamHost enables GZIP compression by default on all web hosting plans, so you do not need to take any further steps to turn it on for your site.

However, please note that SSLCompression is disabled on DreamHost servers by default due to a security vulnerability when combined with HTTPS (using a TLS/SSL Certificate).

You can read more about GZIP Compression and alternatives to use for HTTPS in this article.

DEFLATE is a lossless data compression algorithm similar to GZIP, but it works in Apache 2. In the past, mod_gzip was a recommended tool. But DreamHost uses Apache 2 and mod_deflate, which calls GZIP on the back end automatically:

<ifmodule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</ifmodule>

Browser caching

Using mod_expires, you can tell visiting browsers to hold on to certain files longer (such as images, which are rarely changed):

# BEGIN Expire headers  
<IfModule mod_expires.c>  
  # Turn on the module.
  ExpiresActive on
  # Set the default expiry times.
  ExpiresDefault "access plus 2 days"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/svg+xml "access 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
  ExpiresByType image/ico "access plus 1 month"
  ExpiresByType image/x-icon "access plus 1 month"
  ExpiresByType text/html "access plus 600 seconds"
</IfModule>  
# END Expire headers  

Disabling caching

The examples above display DreamHost's default server settings. However, it may sometimes be necessary to disable caching completely. To do so, add the rules shown below to your .htaccess file.

Disabling the site's cache can significantly impact its speed and optimization scores. It's highly recommended that you leave caching active on your site to ensure it's responding as quickly as possible.

Only proceed with adding the following rules if you are absolutely sure you know what you're doing and understand the consequences. You can test your site speed using some of the websites mentioned in this article.

#Disables GZIP
SetEnv no-gzip 1
    
#Turns off the expires headers for Apache
<IfModule mod_expires.c>
  ExpiresActive Off
</IfModule>

# Disable Caching
<IfModule mod_headers.c>
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires 0
</IfModule>

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?