Magento Expert Forum - Improve your Magento experience

Results 1 to 2 of 2

Speed up your site with .htaccess caching mod_expires and mod_headers

  1. #1
    Administrator david's Avatar
    Join Date
    Nov 2012
    Posts
    264
    Thanks
    22
    Thanked 39 Times in 32 Posts

    Cool Speed up your site with .htaccess caching mod_expires and mod_headers

    Browser cache is very important to speed up your site loading and here is how to speed up by mod_expires and mod_headers.

    Before doing anything let 's make sure 2 of these mod are turned on. Here is how to turn on it on Ubuntu.

    1. Enable mod_expires and mod_headers

    Code:
    sudo a2enmod headers
    sudo a2enmod expires
    Then restart apache to apply the change

    Code:
    sudo service apache2 restart
    or

    Code:
    sudo /etc/init.d/apache2 restart
    2. Add caching code for .htaccess file

    Caching with both mod_expires + mod_headers

    Code:
    # Turn on Expires and set default to 0
    ExpiresActive On
    ExpiresDefault A0
     
    # Set up caching on media files for 1 year (forever?)
    <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
    ExpiresDefault A29030400
    Header append Cache-Control "public"
    </FilesMatch>
     
    # Set up caching on media files for 1 week
    <FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
    ExpiresDefault A604800
    Header append Cache-Control "public"
    </FilesMatch>
     
    # Set up 2 Hour caching on commonly updated files
    <FilesMatch "\.(xml|txt|html|js|css)$">
    ExpiresDefault A7200
    Header append Cache-Control "proxy-revalidate"
    </FilesMatch>
     
    # Force no caching for dynamic files
    <FilesMatch "\.(php|cgi|pl|htm)$">
    ExpiresActive Off
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
    </FilesMatch>
    Caching with mod_headers

    Code:
    # 1 YEAR
    <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
    Header set Cache-Control "max-age=29030400, public"
    </FilesMatch>
     
    # 1 WEEK
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
     
    # 3 HOUR
    <FilesMatch "\.(txt|xml|js|css)$">
    Header set Cache-Control "max-age=10800"
    </FilesMatch>
     
    # NEVER CACHE
    <FilesMatch "\.(html|htm|php|cgi|pl)$">
    Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
    </FilesMatch>
    Caching with mod_expires

    Code:
    ExpiresActive On
    ExpiresDefault A0
     
    # 1 YEAR
    <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
    ExpiresDefault A29030400
    </FilesMatch>
     
    # 1 WEEK
    <FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
    ExpiresDefault A604800
    </FilesMatch>
     
    # 3 HOUR
    <FilesMatch "\.(txt|xml|js|css)$">
    ExpiresDefault A10800"
    </FilesMatch>
    And the last one is bonus. Here is list of time maybe you need to change expired time for your code

    Code:
    #      300   5 MIN
    #      600  10 MIN
    #      900  15 MIN
    #     1800  30 MIN
    #     2700  45 MIN
    #     3600   1 HR
    #     7200   2 HR
    #    10800   3 HR
    #    14400   4 HR
    #    18000   5 HR
    #    36000  10 HR
    #    39600  11 HR
    #    43200  12 HR
    #    46800  13 HR
    #    50400  14 HR
    #    54000  15 HR
    #    86400   1 DAY
    #   172800   2 DAY
    #   259200   3 DAY
    #   345600   4 DAY
    #   432000   5 DAY
    #   518400   6 DAY
    #   604800   1 WEEK
    #  1209600   2 WEEK
    #  1814400   3 WEEK
    #  2419200   4 WEEK
    #  4838400   2 MONTH
    #  7257600   3 MONTH
    #  9676800   4 MONTH
    # 12096000   5 MONTH
    # 14515200   6 MONTH
    # 16934400   7 MONTH
    # 19353600   8 MONTH
    # 21772800   9 MONTH
    # 24192000  10 MONTH
    # 26611200  11 MONTH
    # 29030400  12 MONTH

    View more threads in the same category:


  2. #2
    Junior Member
    Join Date
    Sep 2018
    Location
    United Kingdom
    Posts
    93
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    This article demonstrates 2 wonderful approaches to actualize storing on your site utilizing Apache .htaccess (httpd.conf) records on the Apache Web Server. The two techniques are very easy to set up and will drastically accelerate your site!

Similar Threads

  1. Protect a Directory Using .htaccess
    By david in forum PHP programming
    Replies: 1
    Last Post: 14-03-2019, 07:23 AM
  2. Speed up magento 300% with SSD Hosting
    By shunavi in forum Magento Speed up, Performance and Optimize
    Replies: 11
    Last Post: 18-11-2015, 10:37 AM
  3. How to use .htaccess to redirect www to non-www?
    By speed2x in forum PHP programming
    Replies: 2
    Last Post: 22-06-2015, 01:23 PM
  4. How to speed up Magento
    By david in forum Programming & Development
    Replies: 5
    Last Post: 01-08-2013, 10:08 AM
  5. Prevent JS and CSS caching during deployment
    By ccvv in forum Programming & Development
    Replies: 0
    Last Post: 17-05-2013, 02:04 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •