There are different techniques for speeding up a WordPress website. Maybe you have tried a few of them already. But did you know that you can use the .htaccess file to speed up your WordPress site?
Yes, that is right. This strange file on your web server can play a major role in speeding up your website. This article will show you how to edit WordPress htaccess to speed up your website.
Before I do that, let’s take a moment to understand what the .htaccess file actually is.
What is .htaccess?
.htaccess is the shortened form of “Hypertext Access.” This is an especially created file for configuring your server. This file is used to perform advanced tasks like protecting the admin area, safeguarding other sensitive directories, redirecting users, preventing spammers, speeding up your website, and so on.
I know what some of you are thinking – that’s alright, but why there’s a dot before the file name?
Well, the dot is deliberately put before the name to make it a hidden file. That’s why you won’t find the file in the file manager or FTP client because they won’t show the hidden files by default.
How to Create the .htaccess File?
Unlike the other necessary files on your WordPress site, the .htaccess file won’t be created automatically. The file will only be created when you go to Settings > Permalinks, and click the “Save Changes” button.
At this point, the .htaccess file will be created at the root of your web server. You can use the file manager or FTP client to find the file. Make sure you have chosen to display the hidden files.
How to Edit the .htaccess File?
Once you have created the .htaccess file, editing it is very easy. You can use either the File Manager or the FTP client for this purpose.
If you are using the File Manager, you can use the built-in code editor to insert the necessary code into the file. It is also possible to download the file to your computer, make the changes, and finally, upload it to the root of your server.
On the other hand, if you are using the FTP client, you can’t edit the file online. That means you have to download, edit, and upload the file manually.
You should be very careful while editing the .htacees file. A single mistake on this file can make your whole website disappear. The best approach is to take a complete backup of the file before making any changes.
How to Use .htaccess to Speed Up Your Website?
There are several methods to use the .htaccess file to speed up your WordPress website. Let’s take a look at the most popular methods –
1. Using Gzip Compression
Using Compression in Apache Servers
Google developed a special module named mod_pagespeed for compressing files in Apache servers.
The first step in using mod_pagespeed is to find out whether your hosting provider supports this module. GoDaddy and SiteGround allow the module on all plans, while Hostgator, Inmotion Hosting, and A2 Hosting support this module on their VPS and Dedicated hosting packages.
Once you have made sure that your server has mod_pagespeed installed, add the following code to your .htaccess file to enable the module –
In case your hosting doesn’t support the module, you can use the mod_deflate module. To use this module, add the following code to the .htaccess file –
Using Compression in Nginx Servers
If your website is hosted on a Nginx server, add the following code to the .htaccess file in order to use Gzip compression –
2. Using Browser Caching
Using caching is a proven method of improving the speed of your website. Google PageSpeed Analysis and various page loading speed measurement tools recommend browser cache to enhance the page loading speed.
When a visitor comes to your website for the first time, her browser has to download all the image, script, and CSS files to display your website properly. You can’t do anything about this.
But what if the same visitor comes to your website the next day? By default, her browser will load all the files again. Browser caching allows her browser to save the necessary files in its cache. As a result, she will enjoy a significantly faster page loading on her consequent visits.
Setting up the browser cache requires you to choose an expiry date. While this duration depends on your website, the following code offered by Samuel Santos should be sufficient for most people –
3. Preventing Hotlinking and Spammers
Image hotlinking could be a serious issue for your website performance. When other people hotlinks your images on their websites, they are actually using your resources to show those images.
You can use the .htaccess file to prevent image hotlinking. Use this handy tool to generate the necessary code. Copy the generated code and paste it at the end of your .htaccess file.
If you are in a hurry, here’s the code for basic hotlinking prevention –
Make sure that you have replaced “yourdomain.com” with your own domain name. For more options like allowing specific domains to hotlink, choose which files to protect, and to show a default image instead of the hotlinked image, generate your own code by using the tool mentioned above.
Spamming could be another issue which leads to the reduced performance of your website. Spammers use automated bots, which access the comments file directly, and don’t leave any referrer. You can set up the .htaccess file to detect and block these automated bots by adding this code to your .htaccess file –
Make sure that you have replaced “yourdomain.com” with your own domain.
Ensuring the best loading speed is a vital factor for your website’s performance. It has a huge impact on the search engine ranking for your content as well. Now that you know how to create and edit the .htaccess file to speed up your website, what are you waiting for?
Try the tips mentioned above and let me know how they work out for you. And if you have any other tip about using the .htaccess file to speed up a WordPress site, don’t forget to let me know that too.