How to fix add expires headers error in GTmetrix

After uploading the site information to the host and the passage of time, one of the most common errors we encounter when testing the site speed by GTmetrix is ​​the add expires headers error. As it is clear from the literal meaning of this error, it tells us that for some files that are generally in the site header, we must make their expiration date long, which here means the site cache. When you enter a site, files in different formats start to be downloaded, each of these files has an expiration date and remains in your browser’s memory for a certain period of time to be loaded from the server on subsequent visits. , start loading from your browser.

In this tutorial from Hostfa’s knowledge base, going to how to fix the add expires headers error in GT Matrix , by using this feature you can make files that may not have been changed for more than a few years remain in the browser’s memory until this time. remain a user. In this case, the number of HTTP requests to your server will decrease and increase the speed of the site.

Some users notice under the title There is * a static component without a far-future expiration date. they observe This notification refers to the expiration of two files in the header because, as mentioned before, each item has a specific expiration date to remain in the cache memory. But what is the solution?

Because this date has expired, we have no choice but to change the dates. The browser again sends a request to the server to read the information. These files in memory will be updated and will receive a new date as the expiration date. Until the mentioned date, the files and information will remain in the memory and the problem will be solved. For this case, we act in the following way. To get more information about cache memory, read the following tutorial.

What is cache? How many types of cache do we have?

How to fix add expires headers error in GTmetrix

When you intend to fix the problem of add expires headers in GT Matrix, you should know that each of the files based on their usage should be selected from a correct time interval to stay in the cache memory. What determines this work is based on the file format. If we want to put them in a proper category, maybe using the following method is the right thing for the general sites.

  • Image files in jpg, gif, or png format
  • Image file in ico format ( favicon site )
  • CSS files
  • JavaScript files

In the above category, which is specified based on the file format, usually the favicon of the site does not change much. After that, there are CSS and JS files that are dependent on our own changes. Therefore, for files that may not change even for years, we can make them remain in the cache memory of the browser until this time and in the next visits of the user, instead of loading through the host and server, they will be loaded from the cached memory. In this case a decrease in the number of HTTP requests . Now, if we want to check the files and update the cache until the expiration date, we must use the Enable Expirations feature through the htaccess file. let’s use

GTmetrix

For this purpose, enter your host and after entering the File Manager, look for the htaccess file. look around This file is one of the hidden files. So, if you don’t see it, click on the Settings button located at the top of the file manager screen, and then activate the Show Hidden Files option in the window that opens, as shown in the image below.

🟢🔵 If for any reason you fail to find the htaccess file. Do not use the following tutorial to find the hidden files of the host.

View hidden files in cPanel

GTmetrix

Finally, after the file is shown to you, right-click on it and select the Edit option to edit, then put the following codes at the end of the file .

# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 3 month" 
  • In the above codes, we have this command by htaccess. We issued that the expirations feature should be activated first, which is done in the second line by setting it to ON mode.
  • Then, in the last line marked with ExpiresDefault “access plus 3 months”, we give the order that these files remain in the cache memory for 3 months by default (Default directive). They are loaded through the browser’s memory on every visit.
  • After the three months are over, they will be requested once again through the host and will be stored in the cache for another three months (this time in an updated state).

But we have not yet determined what these files contain. Here we start the example with the fave icon file. In order to place the fav icon file on the site in the cache memory for three months and to activate the add expires headers feature, after using the above codes, the following codes related to this file format should be in the form Enter below.

# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 3 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year" 

In the last line of this code, this command is set for image files that are in the form of favicon to be stored in the cache for one year. In the same way, we must act in this way for other files.

<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On 
# Default directive
ExpiresDefault "access plus 3 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"
# CSS
ExpiresByType text/css "access plus 2 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule> 

In the example of the code above, which is in the form of the final code, and at the beginning and end of it, the IfModule command is used, and for practical mode and to activate the add expires headers feature, we must act as above, the possibility of allowing image files in gif, png format , jpg and jpeg for one month, CSS files for two months and JS files for one year were determined to remain in the browser’s cache memory.

I hope that this training has been of interest to you and that you were able to fix the add expires headers error in GT Matrix by using it. If you have any questions or problems related to this tutorial, let us know in the comments section