Adding Speed to your Website with .htaccess

Website Speed

Website speed plays a major part in ranking your website higher than the competition.  The faster your website’s loading time the more search engines will like it. A good FREE tool to test your website speed is the Google PageSpeed Insights among many others.

If you are running on an Apache server you can speed up your website by an average of 18% by adding  a few lines of code to your .htaccess file.  The code – see below – will enable compression, and instruct client browsers to use cached content for media elements which will speed up the loading of previously loaded pages.

Note: These particular instructions will work only on Apache servers with mod_deflate and mod_expires loaded. To check if these modules are loaded use the following command:

[highlight]#rpm -ql httpd|grep “mod_.*\.so” |awk -F’/’  ‘{print $6}'[/highlight]

This will display a list of modules check to see if mod_deflate and mod_expire is included.

If they are not included you can load both modules in /etc/httpd/conf/httpd.conf.

To do so first make a backup copy:

[highlight]#cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.backup[/highlight]

Open the file with a text editor (vi, vim, or nano)

[highlight]#nano /etc/httpd/conf/httpd.conf[/highlight]

Add the following lines and save

[highlight]LoadModule deflate_module modules/mod_deflate.so
LoadModule expires_module modules/mod_expires.so[/highlight]

Restart apache

[highlight]# service httpd restart[/highlight]

Now you are ready to use mod_deflate and mod_expires to add some speed to your website.

At your website root directory open .htaccess with a text editor and add the following lines:

[highlight]
# compress output
<IFModule mod_deflate.c>
<filesmatch “\.(js|css|html|jpg|png|php)$”>
SetOutputFilter DEFLATE
</filesmatch>
</IFModule>
# Instruct user browsers to use cached media
<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 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 2 days”
ExpiresByType text/js “access plus 1 years”
ExpiresByType text/javascript “access plus 1 years”
ExpiresByType application/javascript “access plus 1 years”
ExpiresByType application/x-javascript “access plus 1 years”
</IfModule>[/highlight]

That’s all you need to do.  You can change the expire time for example “access 1 month” as per your website requirements.

 

  , , , , , , , ,


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>