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:
After you’ve saved your .htaccess file, test your website speed again in GTmetrix.com 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:
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> <filesMatch "\.(css)$"> Header set Cache-Control "max-age=604800, public" </filesMatch> <filesMatch "\.(js)$"> Header set Cache-Control "max-age=216000, private" </filesMatch> <filesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=216000, public, must-revalidate" </filesMatch> <filesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=1, private, must-revalidate" </filesMatch> </ifModule>
Now test your site again in GTmetrix.com 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.