You cannot directly add an Expires header to an HTML file using HTML markup. Expires is an HTTP response header sent by the web server when a browser requests a resource. To configure it, you need to change your server or hosting configuration, such as an Apache .htaccess file, Nginx configuration, PHP response headers, or another caching layer.
Expires headers tell browsers how long a resource can remain in the local cache before it should be requested again. They are commonly used for static resources such as images, CSS files, JavaScript files, fonts, PDFs, and other assets. When a returning visitor already has a valid cached copy, the browser may avoid downloading the resource again, reducing network requests and improving loading performance.
Can You Add Expires Headers Directly in HTML?
No. HTML controls the structure and content of a webpage, but it does not provide a standard HTML element for setting HTTP response headers such as Expires.
For example, this is not a valid way to configure browser expiration:
<meta http-equiv="Expires" content="Thu, 01 Oct 2026 00:00:00 GMT">
Although older approaches have used meta http-equiv, browser caching behavior should be controlled with actual HTTP response headers generated by the server. Expires is fundamentally a response-header mechanism.
The correct configuration should therefore be made where the requested resource is served.
How to Add Expires Headers in Apache
If your website runs on Apache, one of the simplest methods is to use the .htaccess file. Apache’s mod_expires module provides the ExpiresActive, ExpiresByType, and ExpiresDefault directives for this purpose. CatsWhoCode provides a similar Apache configuration for common static file types.
A practical configuration is:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"
ExpiresByType image/avif "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType font/woff "access plus 1 month"
ExpiresByType font/woff2 "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
</IfModule>
Place this configuration in the appropriate .htaccess file, normally in the site’s document root or another directory where Apache allows overrides.
The value "access plus 1 month" means the expiration time is calculated from the time the resource is accessed. You can use different periods for different resource types. Apache also provides ExpiresDefault as a default expiration rule, although applying a blanket expiration period requires care because different resources can have very different update frequencies.
How Long Should You Cache Files?
The correct expiration period depends on how frequently a resource changes.
For frequently updated HTML pages, a very short cache period or no long-lived expiration may be appropriate. Static assets such as logos, images, CSS, JavaScript, and fonts can often be cached for much longer when their filenames change whenever the contents change.
For example, a versioned file such as:
style.2026.css
can safely have a long cache lifetime because a new filename can be generated when the CSS changes.
Without versioning, setting a very long expiration period can cause visitors to continue using an old cached file after you update it.
What About Cache-Control?
Although Expires is still supported, modern caching configurations commonly use the Cache-Control response header as well. Cache-Control provides more precise control over browser and intermediary caching.
For example:
<IfModule mod_headers.c>
<FilesMatch "\.(css|js|jpg|jpeg|png|gif|webp|avif|woff|woff2)$">
Header set Cache-Control "public, max-age=2592000"
</FilesMatch>
</IfModule>
Here, 2592000 represents 30 days in seconds.
Using Cache-Control lets you explicitly define the maximum caching duration through max-age. In many modern configurations, Cache-Control is the primary caching directive, while Expires can be retained for compatibility.
What If You Use cPanel?
On Apache hosting managed through cPanel, you can usually edit or create the site’s .htaccess file through File Manager. cPanel’s documentation specifically describes using the Apache mod_expires module to configure Expires headers and notes that this module is installed by default in cPanel environments.
After modifying .htaccess, clear your website cache and test a resource again. A caching plugin, CDN, reverse proxy, or hosting cache can also affect the final response headers.
What If You Use Nginx?
Nginx does not use .htaccess. Expires settings must be added to the Nginx server configuration.
For example:
location ~* \.(jpg|jpeg|png|gif|webp|avif|css|js|woff|woff2)$ {
expires 30d;
add_header Cache-Control "public";
}
The exact configuration should match your existing server setup. After changing an Nginx configuration, the server configuration must be tested and reloaded.
Can You Add Expires Headers to External Scripts?
Not when the external file is hosted on another server that you do not control.
For example, if your HTML loads:
<script src="https://example.com/script.js"></script>
your server cannot directly change the HTTP response headers returned by example.com. The external server controls those headers.
CatsWhoCode addresses this issue by demonstrating a server-side PHP proxy approach that retrieves approved external JavaScript files and serves them through the site’s own server, allowing response headers to be set locally.
However, proxying third-party resources is not automatically the right solution. It can introduce maintenance, security, licensing, update, and performance considerations. If the third-party service controls its own caching headers, that is generally outside your server’s direct control.
How to Check Whether Expires Headers Are Working
After adding the configuration, inspect the actual HTTP response rather than the HTML source code.
In Chrome, open Developer Tools → Network, select an image, CSS file, JavaScript file, or other static asset, and inspect the Response Headers section.
You may see something similar to:
Expires: Sat, 31 Oct 2026 08:00:00 GMT
You can also check:
Cache-Control: public, max-age=2592000
These headers confirm that the server is sending caching instructions to the browser.
Final Answer
If your question is specifically “how to add Expires headers in HTML?”, the accurate answer is: you do not add them to HTML itself. Configure them on the server that delivers the files. For Apache, .htaccess with mod_expires is a common method; for Nginx, use the server configuration; for PHP-generated responses, use PHP’s header() function. cPanel also provides .htaccess-based configuration for Apache hosting.
The most important consideration is not simply adding an expiration date, but choosing cache durations that match how often your resources change and using versioned asset filenames when applying long cache lifetimes.
Relevant Guide
How To Add An Image From A Folder In Html
How To Add Expires Headers In Html Catswhocode
How To Copy Html Code From A Website
How To Create A Banner In Html
How To Insert A Horizontal Line In Html
What Is The Correct Html For Adding A Background Colour
How To Convert Html To Powerpoint
How To Convert Ipynb To Html
How To Link An Email In Html
How To Open A Link In A New Tab Html
How To Upload Html File To WordPress
How To Add Schema Markup To Html
How To Call A Function In Javascript From Html
How To Center An Iframe In Html
How To Change Plain Text Email To Html
