2

I'm trying to make Google PageSpeed happy, but my .htaccess skills are limited. I'm particularly struggling with browser and proxy caching. Does anyone have a template I can use?

Here's what I have for compression so far:

# JavaScript MIME type issues:
#   1. Apache uses "application/javascript": http://svn.apache.org/repos/asf/httpd/httpd/branches/1.3.x/conf/mime.types
#   2. IIS uses "application/x-javascript": http://technet.microsoft.com/en-us/library/bb742440.aspx
#   3. SVG specification says it is text/ecmascript: http://www.w3.org/TR/2001/REC-SVG-20010904/script.html#ScriptElement
#   4. HTML specification says it is text/javascript: http://www.w3.org/TR/1999/REC-html401-19991224/interact/scripts.html#h-18.2.2.2
#   5. "text/ecmascript" and "text/javascript" are considered obsolete: http://www.rfc-editor.org/rfc/rfc4329.txt

#==================================================================================================
# Compression: http://code.google.com/speed/page-speed/docs/payload.html#GzipCompression
#==================================================================================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE application/atom+xml
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml

    # The following MIME types are in the process of registration
    AddOutputFilterByType DEFLATE application/xslt+xml
    AddOutputFilterByType DEFLATE image/svg+xml

    # The following MIME types are NOT registered
    AddOutputFilterByType DEFLATE application/mathml+xml
    AddOutputFilterByType DEFLATE application/rss+xml

    # Compress JavaScript; make sure to list all possible MIME types for JavaScript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE text/ecmascript
    AddOutputFilterByType DEFLATE text/javascript
</IfModule>
#--------------------------------------------------------------------------------------------------

2 Answers 2

2

Here's a template you can use. I got it from someone, don't know who the original author is but it could very well be output of yslow plugin (http://developer.yahoo.com/yslow/).

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
</IfModule>

# if you don't use filenames to version, lower the css and js to something like
#   "access plus 1 week" or so

<IfModule mod_expires.c>
  Header set cache-control: public
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault                          "access plus 1 month"

# cache.manifest needs re-reqeusts in FF 3.6 (thx Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest       "access plus 0 seconds"

# your document html 
  ExpiresByType text/html                  "access"

# rss feed
  ExpiresByType application/rss+xml       "access plus 1 hour"

# favicon (cannot be renamed)
  ExpiresByType image/vnd.microsoft.icon  "access plus 1 week" 

# favicon (cannot be renamed)
  ExpiresByType image/vnd.microsoft.icon  "access plus 1 week" 

# media: images, video, audio
  ExpiresByType image/png                 "access plus 1 month"
  ExpiresByType image/jpg                 "access plus 1 month"
  ExpiresByType image/jpeg                "access plus 1 month"
  ExpiresByType video/ogg                 "access plus 1 month"
  ExpiresByType audio/ogg                 "access plus 1 month"
  ExpiresByType video/mp4                 "access plus 1 month"

# webfonts
  ExpiresByType font/ttf                  "access plus 1 month"
  ExpiresByType font/woff                 "access plus 1 month"
  ExpiresByType image/svg+xml             "access plus 1 month"

# css and javascript
  ExpiresByType text/css                  "access plus 1 month"
  ExpiresByType application/javascript    "access plus 1 month"
  ExpiresByType text/javascript           "access plus 1 month"
</IfModule>


# Since we're sending far-future expires, we don't need ETags for
# static content.
#   developer.yahoo.com/performance/rules.html#etags
FileETag None

# use utf-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8
# force utf-8 for a number of file formats
AddCharset utf-8 .html .css .js .xml .json .rss
0
1

Your syntax looks okay. Some folks might call me old fashioned for doing so, but I use apache's sample configuration shown here:

Also, I highly recommend putting these rules in your apache configuration files rather than in .htaccess files. This is because when a request comes in, apache has to hunt around for your .htaccess files, read them, and process their configurations on the fly. If you put the configuration in your main apache configuration files instead, it compiles them when apache is first started.

On very busy sites with large amounts of requests per second, using .htaccess files can drag down the speed of your site pretty quickly.

1
  • thanks for the insight. My main problem deals with caching, though; the snippet I gave was just for compression. Any ideas about browser & proxy caching that will make PageSpeed happy?
    – user57386
    Oct 17, 2010 at 20:09

You must log in to answer this question.