How to speed up WordPress site – .htaccess optimization
How to speed up WordPress site – .htaccess optimization.
Nowadays an eye catching look of your website or a perfect content can be not enough to attract the potential users. If you are a site owner, you need to know that Google uses an algorithm which makes a site loading speed one of the factors influencing the site’s position in the search results. So when the site loading time takes a few seconds only, for internet users and google it’s often too long.
Statistics say that almost a half of Internet users expect a site to load in two seconds or less.
It’s important to take care of this, particularly that the results can be improved without much effort.
There are many techniques to improve the loading time of your site. Some of theme are a basic and relatively simple. One of the solutions is a .htaccess file, which allows to introduce the basic site optimization in minutes.
What is a .htaccess file?
.htaccess is a configuration file for use on Apache web servers located in the root directory. It gives the ability to control a specific folder or the piece of server. Using .htaccess file we may modify the configuration of the server software to stop or start running some server functionality and features, for example (taking into account the purpose of this tutorial) we may:
Optimize your site
Improve page loading speed
Create a diversion (internal and external)
Create password protected directories
Block specific IP
And many more
Where to find .htaccess file in WordPress?
The file should be located in the root folder of your WordPress installation.
.htaccess file name starts with a dot (.) at the beginning, which specifies that it is a hidden file. Remember to turn on “show hidden file feature” when you are trying to access your .htaccess file using FTP client or cPanel of your hosting.
Why You can not find .htaccess file?
It may occur that your WordPress site has not generated a.htaccess file yet. It is generated when you set up permalinks on Settings >> Permalinks settings. Simply click on “Save Changes” button to force WordPress to generate a .htaccess file in your site’s root directory.
If at this step you got a problem with file generation, at first check permissions of the directory, must be writable.
Optionally if .htaccess file does not exists, you can create it and upload.
It’s worth to mention that not all servers support using .htaccess file (for example free servers) and the service may be blocked – take it into account when something goes wrong while creating the file.
Optimization code for .htaccess file
The best way to enable compression is using mod_gzip or mod_deflate for Apache software (or equivalent for other web server). Both modules basically do the same thing, compress data, but mod_deflate is better supported and documented, also easier to configure. If mod_deflate doesn’t work correctly on your website or web server for some reason, of course you can use mod_gzip. Both compression methods can be enabled by adding a simple code in WordPress.htaccess file:
By setting browser caching you’re telling browser to keep some kind of files for a specified period of time in local memory (cache). When cached file is needed again, the browser will load from its local drive instead of requesting it from the server.
We can use for that purpose two Apache modules – mod_expires and mod_headers (or equivalent for other web server).
Both methods can be set at the same time in .htaccess file:
Note: There is no need to set the max-age directive with the Cache-Control header since it is already set by the mod_expires module. In other case we should use :