How to add expires headers in the server?

by jacey.lubowitz , in category: SEO , 7 months ago

How to add expires headers in the server?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by dortha , 7 months ago

@jacey.lubowitz 

Adding Expires headers to your server response can improve the speed and performance of your website by allowing the browser to cache certain resources. Here are the steps to add Expires headers in your server:

  1. Enable mod_expires module: If you're using Apache as your web server, you need to enable the mod_expires module. You can do this by running the following command on your server:
1
a2enmod expires


  1. Configure the Expires headers: Once the module is enabled, you need to configure the Expires headers. You can do this by adding the following code to your server configuration file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresDefault "access plus 2 days"
</IfModule>


This code sets different expiration times for different types of resources, such as images, CSS, JavaScript, etc.

  1. Test your configuration: After adding the Expires headers, you should test your configuration to make sure it's working as expected. You can use online tools such as GTmetrix or PageSpeed Insights to check whether the headers have been added correctly.


Note that adding Expires headers may not work in some cases, such as when you have dynamic content that changes frequently. In such cases, you may need to use other caching mechanisms, such as ETag or Last-Modified headers, to improve your website's performance.