Caching files in browser or proxy rather than requesting every time reduces the network traffic between the server, the client.
Usually not everyone has control over their own server configuration file, but sometimes they do have .htaccess file accessibility. You can do efficient caching using the Apache .htaccess file.You need to configure few lines of commands in the .htaccess file instructing the server to handle the caching of different types of files.
Enabling file caching can greatly improve your site’s performance and speed.
- How to create and use .htaccess file? Common usage and risks
- Website or Web page redirection using a .htaccess file?
- How to specify your own ErrorDocuments using .htaccess file?
- Password Protecting website Pages and Directories using .htaccess.
2 Apache modules which are included in the distribution helps in configuring the caching in .htaccess file.They are mod_expires and mod_headers.
They are not turned on by default but you can check it by using run httpd -l; command .This will list available modules.Usually your website hosting environment will have these modules enabled.
If these modules are not enabled and if you have access to the server configuration you can enable this by uncommenting the following or similar lines in the server configuration files.
-enable-module=expires and -enable-module=headers
Using Apache Module mod_expires
Apache Module mod_expires controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server response.Browser checks the headers and if cached, the document may be fetched from the cache rather than from the source until expire time has passed.
For example you can add the following in the .htaccess file
1 2 3 4 5 6 7 8 9
.htaccess Time examples
1 2 3 4 5 6 7
300 5 Minute 3600 1 Hour 86400 1 DAY 259200 3 DAY 604800 1 WEEK 2419200 1 MONTH 29030400 12 MONTH
Apache Module mod_headers-Static content cache headers
This module provides directives to control and modify HTTP request and response headers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Turn on Expires and set default expires to 1 day ExpiresActive On ExpiresDefault A86400 # Set up caching on media files for 1 month <FilesMatch ".(ico|gif|jpg|png|flv|pdf|swf|mov|mp3)$"> ExpiresDefault A2419200 Header append Cache-Control "public" </FilesMatch> # Set up 1 Hour caching on commonly updated files <FilesMatch ".(xml|txt|html|js|css)$"> ExpiresDefault A3600 Header append Cache-Control "private, must-revalidate" </FilesMatch>
Cache-control: public means the cached version can be saved by proxies and other intermediate servers.
Cache-control: private means the file is different for different users and their private browser can cache it, but not public proxies.
Cache-control: no-cache means the file should not be cached.
This was a very limited overview of a cache setup for your Apache website.Please let me know your Comments, thoughts and if I am wrong my mistakes or any additions.
Before you go, subscribe to get latest technology articles right in your mailbox!.
Comment on “Speed Up websites by enabling .htaccess Caching using mod_expires and mod_headers”
Can you turn on keep alive in .htaccess ??