When visiting a website, the website will cache in your browser. This cache can be controlled by configuring your Cache-Control HTTP headers for your website This is done by adding mod_expires in the .htaccess file of your server.

If you don't set your Cache-Control for the HTTP headers, then, you will have a longer wait times when visiting your website. Each time your website is accessed without Cache-Control, your website has to make a request to the server for each image, javascript file, CSS file, and so forth to load.

Browser caching is good because it allows your web resources to be stored in the browser for faster page load time. This makes your site faster to surf on the web. To remove the cache from your browser, your browser cache needs to be cleared. For information on clearing your browser cache, please see our article on How to clear your browser's cache. The following sections will explain the basics of setting up the Apache Module mod_expires in your .htaccess.

Basic code for setting expire dates for cache

In order to add browser caching to your website, you will need to set the date for when the cache expires. This cache code is placed in the .htaccess found in your public_html folder. You will need to edit your .htaccess file. Add the following code to the file and save it.

ExpiresActive On
ExpiresByType image/jpg "access plus 1 year"
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/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 2 days"

Now your site will set the time each for each resource that was added to the .htaccess to expire. In the previous code example, the jpg, jpeg, gif, png will expire in a year and pdf, javascript and flash files will expire in a month. More explanation o these settings will come later in this article. Next we will look at using the <ifmodule> for adding Cache-Control with mod_expires.

Code for mod_expires in an <ifmodule>

Using mod_expires in an <ifmodule> allows image types and other file types to be set in an array. This matches the file types to the specific expire time. This streamlines the htaccess code. In the code below, the file types are listed in a row like jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3. Below is an example of the code to use.

<ifmodule mod_expires.c>
<Filesmatch "\.(jpg|jpeg|png|gif|js|css|swf|ico|woff|mp3)$">
    ExpiresActive on
    ExpiresDefault "access plus 2 days"

This sets the cache to expire to the same duration for each of the file types. To specify the specific times for each individual file type, you will want to use the previous code example. The following will explain the Directive types that are used for Cache-Control.

Directive types

There are three directive types; ExpiresActive, ExpiresByType, and ExpiresDefault. The following table explains the difference between them.

Caching directives
ExpiresActive Directive Enables the Expires headers for the website.
ExpiresByType Directive This defines the age of the cache header and the type of file to cache.
ExpiresDefault Directive This sets the age of the cache for all documents other than those specified in the ExpiresByType for the site.

Caching Directive bases

There are 3 base types the access, now, and modification type.

  • access
  • now (same as "access")
  • modification

Duration of cache time

The duration of cache time can be set to one of the following units of time.

  • years
  • months
  • weeks
  • days
  • hours
  • minutes
  • seconds

Basic syntax for ExpiresByType intervals

The following shows the basic syntax for the ExpiresByType. Each file type can be set to specific times to expire the cache.

ExpiresByType text/html "access plus 2 days 12 hours"
ExpiresByType image/png "access plus 6 months 3 days"

Recommended Expire date ranges

You do not want to set your expire times for your cache to unrealistic settings. If you keep the cache to a maximum of a year and a minimum of a month, you should have your browser caching working optimized for your site.

  • Set your images to a long expire time like "access plus 1 year". Images take more time to load and are updated less frequently than other files.
  • Make your CSS, HTML, and Javascript expire at a minimum of a month like "access plus 1 month". CSS, HTML and JavaScript's typically are updated more when developing a site than the sites images.
  • Keep your cache expire date at most a year.
Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
2014-07-23 9:59 pm
I added the Basic expire code from this tutorial to my htaccess file and the GTMetrix Report is still saying "F" fail on that issue...so it must not be seeing it. Is the code changed to adding expire dates to the headers ? I do add this code to the htaccess file and that the only thing to do ? or is there another step ?
8,769 Points
2014-07-24 10:35 am
Hello PaulAlford,

Thank you for your question. We are happy to help, but will need some additional information. What is the exact issue and suggestion GTMetrix is giving you an "F" for?

It should begin as soon as you add the .htaccess rule. If you are using a CMS, such as Wordpress, Joomla, or Drupal, it's possible an existing rule in the .htaccess file is interfering with the rule you are adding.

If this is the case, you would instead go to the specific setting/plugin in the CMS, and set it there.

If you have any further questions, feel free to post them below.
Thank you,

n/a Points
2014-08-01 3:47 pm

What is it? Set-Cookie: PHPSESSID=kvtgebg048oc30mvuqnkeiviq2; path=/

Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=604800, must-revalidate
Pragma: no-cache


Why the date Expires in past? The server answer with the included script of jcart on a site artchern.com gives such answer lust date, having cleaned this script the server answer at once becomes true and Expires: shows date for a week forward. Any instructions in accesse, httpd don't give the decision. In the script there are no direct instructions Cache-control (no cache) on a caching ban. well or they are written by any hidden formulas? or it is meant that the POST method programmatically forbids a caching? In what area to look for? Thanks.

9,968 Points
2014-08-01 4:36 pm
Hello Mike, and thanks for your comment.

The Set-Cookie: PHPSESSID part is simply PHP setting a cookie for the session of a user.

I'm not sure why your Expires header would be showing a date in the past, what is the code you currently have in your .htaccess file for turning on expires headers? That is not normal behavior, so it seems like it's either something with your code or your server that is causing this.

- Jacob
n/a Points
2014-08-03 2:29 am

Hello JacoblMH! Thanks for the answer.In my .htaccess file  currently this:AddType application/x-httpd-php .php .htm .html

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.artchern.com$ [NC]

RewriteRule ^(.*)$ http://artchern.com/$1 [R=301,L]


RewriteRule ^news_([a-z_0-9]+).html$ news.html?id_news=$1 [L]

<IfModule mod_expires.c>

ExpiresActive On

ExpiresDefault "access 7 days"

ExpiresByType image/jpeg "access plus 7 day"

ExpiresByType image/jpg "access plus 7 day"

ExpiresByType image/png "access plus 6 months"

ExpiresByType image/gif "access plus 6 months"

ExpiresByType image/x-icon "access plus 6 months"

ExpiresByType application/javascript "access plus 6 months"

ExpiresByType text/css "access plus 6 months"

ExpiresByType text/javascript "access plus 6 months"

ExpiresByType text/js "access plus 6 months"


</IfModule>and in the file httpd.conf   contains this: 

RewriteEngine on

RewriteRule ^/(.*\.)v[0-9.]+\.(css|js|gif|png|jpg)$ /$1$2 [L,E=VERSIONED_FILE:1]

Header add "Cache-Control" "max-age=604800" env=VERSIONED_FILE

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf|xml|txt|html|htm)$">

Header set Cache-Control "max-age=604800, public"



 I wrote above that on a site there is a script "jcart"

  and if absolutely to clean this script "jcart" then the answer of the server


   will be correct with the necessary date forward.ThanksMike

8,378 Points
2014-08-04 10:57 am
When viewing your headers, everything looks fine to me and I'm not seeing any dates in the past. It does appear that you may have some server side caching issues that you may want to speak with your server admin about. Unfortunately, I was unable to replicate the issue you are seeing at all.
n/a Points
2014-08-26 6:07 am


We are having excel files in docroot which will be often updated.

Whenever we updated the content of the .xls file we are not seeing the modifed content  while accesing through the URL. No script or program we are using. We just putting the file in docroot and access through the URL.

I feel like its caching somewhere may be in proxy.

I used mod_header to stop cacheing with below entry in httpd.conf file but still we are not getting the updated content, it takes time to get the updated content. COuld someone help me to fix it.

<FilesMatch "\.(xls)$">FileETag None#<ifModule mod_headers.c>Header unset ETagHeader set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"Header set Pragma "no-cache"Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"#</ifModule></FilesMatch>

18,098 Points
2014-08-26 9:06 am
Hello Abdur,
It sounds as if you simply do not want the xls files to cache. Try using the code below as it should set th xls files to a non caching state.

<FilesMatch "\.(xls)$">
ExpiresActive On
ExpiresDefault A1
Header append Cache-Control must-revalidate

Kindest Regards,
Scott M

Post a Comment

Email Address:
Phone Number:

Please note: Your name and comment will be displayed, but we will not show your email address.

8 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?


Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!