Improving web site performance with Apache .htaccess
Speed optimizations can be easily done with simple .htaccess rules. These rules can make any website faster by compressing content and enabling browser cache. .htaccess is a special configuration file which you should be able to dig up in the root of your server directory. With .htaccess file you can create redirects, rewrite URLs on your website, create password protected directories and other technical things, but you can also use them to improve your site’s page loading speed by taking advantage of some Apache server features.
Compression reduces response times by reducing the size of the HTTP response. It’s worthwhile to gzip your HTML documents, scripts and stylesheets. Gzip compression helps to reduce the amount of data your server needs to send to your users, which in turn speeds up the load time of your site.
Code Block |
---|
language | xml |
---|
theme | DJango |
---|
title | Try adding this in a .htaccess file |
---|
|
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule> |
You can use the Apache mod_deflate module to compress output from your web site that is sent to client browsers.
Code Block |
---|
language | xml |
---|
theme | DJango |
---|
title | Try adding this in a .htaccess file |
---|
|
# Enable Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
</IfModule> |
When a new visitor enters your website all the files like scripts, CSS styles and images are downloaded – there is no way to avoid it and it naturally produces several requests to your server. But what if this visitor comes back to your website later? We can tell the user’s browser to store these files in its cache; then, when the user returns to your site it will reload the files from the cache rather than downloading them all again.
In order to add browser caching to your website, you will need to set the date for when the cache expires.
Code Block |
---|
language | xml |
---|
theme | DJango |
---|
title | Try adding this in a .htaccess file |
---|
|
# Leverage Browser Caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule> |
Browser cache control is directly related with the header expires date and according to the Apache documentation provides directives to control and modify HTTP request and response headers. Headers can be merged, replaced or removed. You can use this feature by including this code in your htaccess
Code Block |
---|
language | xml |
---|
theme | DJango |
---|
title | Try adding this in a .htaccess file |
---|
|
<IfModule mod_headers.c>
Header set Connection keep-alive
<filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
Header set Cache-Control "max-age=2678400, public"
</filesmatch>
<filesmatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, private, must-revalidate"
</filesmatch>
<filesmatch "\.(pdf)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
<filesmatch "\.(js)$">
Header set Cache-Control "max-age=2678400, private"
</filesmatch>
</IfModule> |
Warning |
---|
title | How do I enable .htaccess in AbanteCart? |
---|
|
After installing AbanteCart an .htaccess file is already created, but it’s called htaccess.txt and looks like a regular text file. The reason for this? Compatibility. There’s no guarantee that every server that AbanteCart is installed on will support .htaccess features. Don’t worry though; most modern servers that are built to support AbanteCart will also support .htaccess You just need to rename the htaccess.txt file to .htaccess (remember about the dot that comes before “htaccess”!). |