6
\$\begingroup\$

Here is my standard .htaccess file, any suggestions on how I can improve it with regards to page load speed? Would gZip instead of DEFLATE make any sort of difference?

ExpiresActive On
RewriteEngine on
RewriteBase /

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

# Expires 1 month after file is first accessed
ExpiresByType image/jpeg A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType text/plain A2592000

# Expires 1 month after file is modified
ExpiresByType application/x-javascript M2592000
ExpiresByType text/css M2592000
ExpiresByType text/javascript M2592000
ExpiresByType text/html M2592000
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Honestly, this looks OK, though you could clean it up a little bit.

You have two ways to make this code better:

  1. group related mime types.
  2. use human-readable syntax

Regarding point #2, here's part of the config I created after watching Illya Grigorik's presentation "Breaking the 1000ms Time to Glass Mobile Barrier":

<IfModule mod_deflate.c>
# these are known to be safe with MSIE 6
AddOutputFilterByType DEFLATE text/html text/plain text/xml

# everything else may cause problems with MSIE 6
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/x-javascript application/javascript application/ecmascript
AddOutputFilterByType DEFLATE application/rss+xml

ExpiresActive On
ExpiresDefault "access plus 15 minutes"

ExpiresByType text/html "access"
ExpiresByType application/json "access"

ExpiresByType text/css "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>

You might also look into Google's mod_pagespeed for Apache.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Hello John, and welcome to Code Review! I've stumbled upon (and upvoted) a few of your answers since yesterday, and they're usually very good! It's always great to see new active members :). However, please avoid just posting a random dump of yours that seems related. In this case, you could simply have said "You have two ways to make this code better: 1/ group related mime types 2/ use this human-readable syntax". I believe such an answer would have been even more helpful than the current one. And again, thank you for your answers! \$\endgroup\$ May 15, 2013 at 8:05
2
\$\begingroup\$

You can improve this code by removing unnecessary repetition. If you put a trailing slash after each declaration, then you do not need to repeat the same code over and over again. I would also suggest wrapping all the similar statements inside of IfModule checks: it only takes the server a fraction of a second, maybe less, to check if a module is installed; if it is not, it will skip straight past that section of the code.

ExpiresActive On
RewriteEngine on
RewriteBase /

<IfModule mod_deflate.c>
# compress text, html, javascript, css, xml:
    <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE text/plain \
                          DEFLATE text/javascript \
                          DEFLATE text/html \
                          DEFLATE text/xml \
                          DEFLATE text/css \
                          DEFLATE application/xml \
                          DEFLATE application/xhtml+xml \
                          DEFLATE application/rss+xml \
                          DEFLATE application/javascript \
                          DEFLATE application/x-javascript 
    </IfModule>
</IfModule>

<IfModule mod_expires.c>
    # Expires 1 month after file is first accessed
      ExpiresByType image/jpeg A2592000 \
                    image/jpg A2592000 \
                    image/gif A2592000 \
                    image/png A2592000 \
                    image/x-icon A2592000 \
                    text/plain A2592000 \

    # Expires 1 month after file is modified
      ExpiresByType application/x-javascript M2592000 \
                    text/css M2592000 \
                    text/javascript M2592000 \
                    text/html M2592000 \
</IfModule>

There honestly wouldn't be a noticeable difference between GZip & DEFLATE: the only difference is that GZip uses a checksum, header & footer which are also included. Hence a tiny bit slower, but more reliable and secure.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.