Select Sidearea

Populate the sidearea with useful widgets. It’s simple to add images, categories, latest post, social media icon links, tag clouds, and more.

How to Add Expires Headers in WordPress?

How to Add Expires Headers in WordPress?

How to Add Expires Headers in WordPress?

Did you know that adding an Expires header to your WordPress site can drastically improve the page speed? WordPress comes by default with a caching plugin called mod_rewrite, which comes in really handy when you want to speed up your site. You can use this for a better user experience and increase your rankings in Google.

Adding Expires headers to your WordPress blog returns several advantages. First of them is faster loading pages for the visitors (less data transferred means less time to download in bytes). Second, you will get higher response rate, since many search engines consider a lot of factors when ranking web sites – one of those being page load speed. So I thought, why not write a post about it?

What is Browser Caching?

Basically, browser caching is an organization of files on your computer that helps improve page loading time in your web browser. We all experience delays when using the Internet. When your computer has to download a file from the web, it can take a slightly longer time because the server has to return a cached copy of the information. If you make a change to your WordPress site and save a copy of that file, however, this can cause problems since your edits aren’t instantly reflected in the browser.

The benefit of browser cache is not limited to speedier loading times, however. With the right set of headers you can also increase the amount of time a cached page retains in your cache, thus improving page information access times without affecting the overall interactive viewing experience.

There are several reasons why your WordPress website might not show you the cached content from your browser. Caching is an optimization process that helps the web servers serve your site faster. There are two ways to add headers to cached pages, and both allow you to control when the page is cached. Either way, if you’re experiencing problems with cached content, it’s a good idea to know the basic structure of the WordPress process and how to work around problems like this one.

What Are Expires Headers?

Expires headers (or last-modified headers) are used to indicate to the browser that a document has been updated. If you remove the expires header from a WordPress post or page, the browser will display the document as updated, even though it hasn’t actually been modified. This is why it’s important to implement expires headers on every page that you update… and why it’s a good idea to do so immediately after creating the page or updating its contents.

Expires headers are a kind of header tag that WordPress uses to delay the time a page is considered stale. Stale content remains on the page until a user clicks to see the content. Expires headers can be useful if you want to avoid caching content that might be outdated, or if you want to have certain content remain on a website even if it has been removed from the list of available posts. In either case, if you aren’t sure whether an expiry header is appropriate for your purposes, you should look into whether there is a better solution for reaching the intended goal.

Expires Headers vs cache-control

Expires headers are a meta tag that informs WordPress how long ago a chunk of content was published. If a plugin has an option to add an expires header, then the plugin is telling the browser that the content might not be retrievable after a set time period. This can be used to time-out or expire content automatically when certain events occur. The problem is that some browsers (including Firefox and Google Chrome) have a strict limit on how long they will keep cached pages, so when you edit a page that has an expires header, the update will not show up in the browser until the Cache-Control header has been removed or the plugin has been updated to include a newer version of its core code.

Expires headers are a useful way to tell the browser that a link or file has expired. They help webmasters save bandwidth by telling the browser when its cached copy of a web page might be outdated. If you create a lot of static files (such as pages) and don’t want browser cache to hold up your site, expiring headers can help. It’s also worth noting that some hosts (such as leetoo.com) will automatically set expire headers for all links matching a regular expression. Use this feature when you need to ensure your files aren’t kept around too long.

How to Fix “Add Expires Headers” in WordPress?

If you are a WordPress user, you may have experienced the problem of having links to your website expire. This is a common problem with hosting companies and can be avoided by changing your domain name. Let’s look at some of the reasons why these headers may be causing problems and how to fix them. Add Expires Headers in WordPress has been around since 2011 and although it has been removed from most WordPress themes, it is back in full force in 2017. There’s a script that allows you to setup your domain to always have this header, along with other useful information, such as the IP number of your site.

How to Add Expires Headers With .htaccess on Apache Web Server?

If you are using WordPress, there is a built-in option to add expires headers to your files. The expires header can be used to tell browsers when a file has been read but no modification has been made to it. It is commonly used for tracking the last-modified time of files on disk or in local storage. You may have heard this header applied to all sorts of parts of your website, including internal links and database files. However, it is also commonly seen attached to files within your WordPress core files.

Here’s what you need to do:

  1. Connect to your server using FTP in your preferred FTP client
  2. Locate the .htaccess file in the root folder
  3. Download a backup copy of the .htaccess file to your local computer. That way, if anything goes wrong, you can reupload your backup copy to fix any potential problems.
  4. Add the code snippet below near the top of the file
## EXPIRES HEADER CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/svg "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES HEADER CACHING ##

While the default values are working well for most of the sites, you can adjust the time periods for different types of files in the code snippet according to your requirements.

How to Add Expires Headers With config File on Nginx Web Server?

Expires headers are a obsolete mechanism for adding a time-to-live (TTL) header to your responses to clients browsers. The original purpose was to reduce network usage by caching old responses, but that technique has become largely redundant in modern web browsers where most clients automatically respond with the cached results when they receive new responses with a Time-to-Live (TTL) header.

If your host using Nginx server, you can control Expires headers by editing your server’s config file.

Here’s the code that you need to use:

location ~*  \.(jpg|jpeg|gif|png|svg)$ {
		expires 365d;
	}

	location ~*  \.(pdf|css|html|js|swf)$ {
		expires 2d;
	}

You can adjust the expiration time for different types of files as requested.

How to Add Expires Headers With a WordPress Plugin?

If you aren’t comfortable with adding the expires headers code yourself, you can also find some WordPress plugins to do the job. Some of these plugins add expires headers, while others handle cache-control. Either way, the end result is that your WordPress site can have good performance from browser caching.

Here are some other cache plugins that can help you in implementing expires headers and/or browser caching:

  1. WP Super Cache
  2. W3 Total Cache
  3. WP Fastest Cache

If you already have caching mechanism and just want a more targeted plugin, two good free plugins are:

  • Leverage Browser Caching
  • Add Expires Headers

In the Leverage Browser Caching plugin has no settings – you just activate it and it starts working instantly.

How to Add Expires Headers for External Scripts?

Most of the WordPress sites rely on Google Fonts for custom fonts. It is basically involves loading the fonts from Google’s CDN, which means you won’t be able to set Expires headers via web server’s configuration.

One easy fix is to locally host the font files, instead of relying on Google’s CDN. Set this up, as you can use the free Optimize My Google Fonts (OMGF) plugin.

How to Add Expires Headers to Google Analytics?

If you’re seeing issues related to browser caching with Google Analytics, you can also consider locally hosting the Google Analytics script, just as we showed you with Google Fonts.

Here are some WordPress plugins that make it easy to achieve this:

  • CAOS (Complete Analytics Optimization Suite)
  • Perfmatters
  • WP Rocket comes with Google Tracking add-on.

Conclusion

You have been searching for hours, and now you are at the end of it. You have read thousands of posts, and yet, you are still not completely sure about this particular closing. You think maybe there is some other trick that will enable you to get the job done, but you keep coming back to the same thing: You need to add Expires Headers in WordPress.

Write a comment