Optimize your website by using htaccess file is a very easy and fastest way 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 a 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 your site using Google PageSpeed Analysis tools or some other optimization tools you maybe receive some advice to use browser cache to get a speed boost on 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 to 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 up to 70% to 80%. htaccess file is one of the best search engine optimization tools.
Listed here are just a few more articles, which will help to learn more about WordPress issues and fixes.
- Best SEO Tips 2018 to get higher search ranking on Google
- How to Uninstall and Delete a WordPress Theme?
- WordPress SEO Tips to Boost Your Website Ranking
- How to Add a Click-to-Call Button in WordPress?
- How to Fix Syntax Errors in WordPress?
- How to enable the Cloudflare Development Mode?
- How to exclude a category from a WordPress blog page
- How to Fix HTTP error on Image Upload in WordPress?
I hope this article helps to optimize your website with the help of htaccess file. If in case you have any further questions, be happy to ask me in the feedback part below.