How can I cache my site with an .htaccess file?

Creating an .htaccess file on your DreamHost web server

View the following article for instructions on how to create an .htaccess file on your web server:

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

GZIP

Due to certain security vulnerabilities found when combining HTTPS and GZIP compression, GZIP compression is disabled for HTTPS on all DreamHost plans. GZIP compression is automatically enabled for HTTP, however.

You can read more about GZIP Compression and alternatives to use for HTTPS in the following 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.

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

<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>

View the following page for further details:

Browser caching

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

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

# 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. This can be accomplished by adding the following rules to your .htaccess file.

Disabling cache could significantly impact your site speed and site 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 here:

#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.