PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites

19 Nov 2013

How to add expires headers using .htaccess

                      Check your site in  google insights page speed. You will get a error "Leverage Browser Caching", if your server doesn't add caching headers to your site. If you add expires headers to your .htacces file, then you will get better site speed. Expires headers tells browser whether they should request file from browser or whether they should grab it from browser's cache. Its not only reduce the loads of download from server but rahter to reduce the number of http requests for the server.
Example:
        <IfModule mod_expires.c>
             # Enable Expiration
                ExpiresActive On

             # Default Directive
                ExpiresDefault "access plus 1 month"

             # Favicon
                ExpiresByType image/x-icon "access plus 1 month"

             # Images
                ExpiresByType image/jpeg "access plus 1 month"
                ExpiresByType image/jpg "access plus 1 month"
                ExpiresByType image/png "access plus 1 month"
                ExpiresByType image/gif "access plus 1 month"

             # Css
                ExpiresByType text/css "access plus 1 month"

             # Javascript
                ExpiresByType application/javasacript "access plus 1 month"

             # Html
                ExpiresByType text/html "access plus 1 month"

             # Xml
                ExpiresByType application/xhtml+xml "access plus 1 month"

             # Manifest files
                ExpiresByType application/x-web-app-manifest_json "access plus 1 month"
                ExpiresByType text/cache-manifest "access plus 1 month"

             # Media
                ExpiresByType audio/ogg "access plus 1 month"
                ExpiresByType video/mp4 "access plus 1 month"
                ExpiresByType video/ogg "access plus 1 month"
                ExpiresByType video/webm "access plus 1 month"

             # Web feeds
                ExpiresByType application/atom+xml "access plus 1 month"
                ExpiresByType application/rss+xml "access plus 1 month"

             # Web fonts
                ExpiresByType application/font-wof "access plus 1 month"
                ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
                ExpiresByType application/x-font-ttf "access plus 1 month"
                ExpiresByType font/opentype "access plus 1 month"
                ExpiresByType image/svg+xml "access plus 1 month"
        </IfModule>

Explation:
            Where,
                   mod_expires.c allows setting by file type to control how long browsers cache file.
                   ExpiresActive enable the expires.
                   ExpiresByType represents the type of file like image, html ...
                   "access + 1 month" means how long the file type cached by browser.

Related Post: