Skip to main content

htaccess file

htaccess file help to optimize your website for improve ranking

Optimize your website by using htaccess file is very easy and fastest way to to improve your website’s loading speed. .htaccess can help to optimize a website for search engines and it helps to improve search engine rankings. It also can be used to improve the security of your website. Every web developer should know that there are many useful codes for htaccess file to optimize website.

Enable gzip compression

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 website. Gzip compresses common strings, this can reduce the size of pages and style sheets by up to 70% to 80%!

If your hosting provider has mod_gzip module enabled on your host, the best way to compress your content is to add the following lines to your .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.*

After you’ve saved your .htaccess file, test your website speed again in to make sure it has been properly compressed.

Use .htaccess file to set Expires and Cache-Control headers

If you test you site using Google PageSpeed Analysis tools or some other optimization tools you maybe receive some advice to using browser cache to get a speed boost your website. When a new visitor opens your website all the files like scripts, CSS styles and images are downloaded – there is no way to avoid it and it naturally generates several requests to your server. But what if this visitor comes back again to your website later? Then we can tell the user’s browser to store these files in its cache; when the user returns to your site it will reload the files from the cache rather than downloading them all again from your server.

To set your Expires headers add these lines to your .htaccess:

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"

Expires Headers helps increase load times for returning visitors on your website.

To set Cache-Control headers add:

<ifModule mod_headers.c>
  <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  <filesMatch "\.(css)$">
    Header set Cache-Control "max-age=604800, public"
  <filesMatch "\.(js)$">
    Header set Cache-Control "max-age=216000, private"
  <filesMatch "\.(xml|txt)$">
    Header set Cache-Control "max-age=216000, public, must-revalidate"
  <filesMatch "\.(html|htm|php)$">
    Header set Cache-Control "max-age=1, private, must-revalidate"

Now test your site again in or Google PageSpeed Insights which is increased its loading speed upto 70% to 80%. htaccess file is one of the best search engine optimization tools.

Leave a Reply

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

Translate »