Categories
Blog Tutorials

How to set Expires Headers in WordPress without Plugins

Caching is a critical thing for websites nowadays. Especially when you have a large website, which will need to repeat the same process of acquiring requests on a visit.

Setting up expires headers in WordPress without plugins is a relatively simple and fast way to speed up your website.

Except for WordPress, many people choose competitive CMS solutions for personal reasons, so pleasing both WordPress and non-WordPress users can ensure a better user experience for everybody.

What are expires headers for WordPress?

Expires headers help website owners to reduce repetitive requests. When someone comes to your website, a request will not be generated. Instead, the page requested will be pulled from the cache itself.

They have a set expiration time, which dies after the set period, but does not end at this point. Once the cache dies, a new one will be automatically generated.

Once a visitor comes to your website, he will receive your website in a cached variant, which loads faster and will not cause your server to suffer from each request.

Caching nowadays relies on the expires headers, and especially WordPress caching plugins contain them.

Every single plugin will have them, so if you don’t wish to disregard them, you can uncheck their cache, or speed up your website without plugins.

As a fair example, WP Rocket and LS Cache do recommend the usage of expires headers. On top of that, they recommend file localization, which takes external scripts and threats them as they are on your server.

As HTTP 1.1 expires headers were deprecated and not recommended for usage anymore, but it does not stop anybody from utilizing them if they want a faster website.

For what should expires headers be used?

Websites hold a lot of data. Aside from the text, which is formatted in paragraphs, line breaks, headings, and even tables, we also have themes and plugins with tons of code.

When a visitor comes to your website, the server will trigger several requests and take some time to deliver your content to that visitor.

Depending on how heavy and optimized your WordPress website is, the visitor will have to wait until the content is received and interactive.

These metrics can be tracked from Google PageSpeed Insights and are used to measure the health and speed of your website.

As another example, GTMetrix is also a helpful tool, which displays how each part of your website is loaded and how much time it took for it to be loaded.

With GTMetrix you can track the requests made, time is taken, and ensure your website is not bloated.

The good thing about GTMetrix is that you get all the sources, which can slow down your website and give you a slight idea of how you can improve them.

However, expires headers cannot be used for external files or cache them because they may interfere with the script and results from the 3rd party.

If you are worried about bad scores from certain scripts, you can remove them from your website, or leave them aside and work on more important elements.

Third-party scripts usually have their own caching set and do not require any additional caching.

How to set Expires Headers in WordPress without Plugins

Setting up expires headers in WordPress is really easy. All you need is access to the server panel and search for the .htaccess file, located in the root directory of your WordPress site.

In order to get there, you will first need to get to your server panel. After that, you can locate and open the File Manager.

Choose the public_html folder and you should be able to see the htaccess file.

If you do not find it in the root directory, you can try to search for it in your domain name folder.

Once you have opened your htaccess file, the next thing you need to do is to add the following lines of code after the closing lines.

# START EXPIRES CACHING #
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType audio/ogg "access plus 1 year"
ExpiresByType video/mp4 "access plus 1 year"
ExpiresByType video/ogg "access plus 1 year"
ExpiresByType video/webm "access plus 1 year"
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/x-component "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType application/font-woff "access plus 1 month"
ExpiresByType application/font-woff2 "access plus 1 month"
ExpiresDefault "access 1 month"
</IfModule>
# END EXPIRES CACHING #

You can change any of these as you like, but keep in mind that if you lower the caching period, your cache will have less time to live.

If you have issues when trying to open your htaccess or your website suddenly started crashing, you will need to fix it, as you may have a corrupted htaccess file.

Conclusion

If you are a WordPress speed expert, you may try to tweak these settings, although they are set to match great speed and good results.

For non-techies the best advice would be to try a caching plugin and optimize websites manually on a website for testing purposes.