Do I need to add Expires header into .htaccess file?

Chris Worner

Well-known member
Joined
Apr 15, 2016
Messages
287
Best answers
0
Ratings
25
Points
28
#1
I read a blog post and they are guiding to add Expires header to end of .htaccess file

Code:
<IfModule mod_expires.c>

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 month"

# My favicon
ExpiresByType image/x-icon "access plus 1 year"

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month"

# Javascript
ExpiresByType application/javascript "access plus 1 year"

</IfModule>
As they are saying, this will take advantage of browser caching and minimizing HTTP requests to the server by using static files such as images, css, javascript stored in the browser cache when you visit the website for the first time. My question is, do I really need to add these codes into my htaccess file?

Thanks in advance.
 
Joined
Apr 25, 2018
Messages
19
Best answers
0
Ratings
7 3
Points
3
#2
I mean, do you want to take advantage of caching by this method? If you do, which I don't see why you wouldn't, then yes add it to your .htaccess file.
 

VirtuBox

Global Mod
Staff Member
Joined
May 3, 2016
Messages
1,503
Best answers
4
Ratings
382 10
Points
83
#4
Best answer
I read a blog post and they are guiding to add Expires header to end of .htaccess file

Code:
<IfModule mod_expires.c>

# Enable expirations
ExpiresActive On

# Default directive
ExpiresDefault "access plus 1 month"

# My favicon
ExpiresByType image/x-icon "access plus 1 year"

# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"

# CSS
ExpiresByType text/css "access 1 month"

# Javascript
ExpiresByType application/javascript "access plus 1 year"

</IfModule>
As they are saying, this will take advantage of browser caching and minimizing HTTP requests to the server by using static files such as images, css, javascript stored in the browser cache when you visit the website for the first time. My question is, do I really need to add these codes into my htaccess file?

Thanks in advance.
To answer to your question, I will split it into two questions :

Should you allow browser caching of static assets ?
yes, you should. Allowing browser to cache static assets will improve your website loading speed (not for the first page), especially on mobile devices with slow internet connection.

Should you add these code into your .htaccess file ?
If your server is running with Apache (and not Apache + Nginx, or Nginx), yes, you can even allow cache longer for images.
Here an example :

Code:
# Expires headers (for better cache control)
<IfModule mod_expires.c>
ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault                          "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest       "access plus 0 seconds"

# Your document html
ExpiresByType text/html                 "access plus 0 seconds"

# Data
ExpiresByType text/xml                  "access plus 0 seconds"
ExpiresByType application/xml           "access plus 0 seconds"
ExpiresByType application/json          "access plus 0 seconds"

# Feed
ExpiresByType application/rss+xml       "access plus 1 hour"
ExpiresByType application/atom+xml      "access plus 1 hour"

# Favicon (cannot be renamed)
ExpiresByType image/x-icon              "access plus 1 year"

# Media: images, video, audio
ExpiresByType image/gif                 "access plus 1 year"
ExpiresByType image/png                 "access plus 1 year"
ExpiresByType image/jpeg                "access plus 1 year"
ExpiresByType video/ogg                 "access plus 1 year"
ExpiresByType audio/ogg                 "access plus 1 year"
ExpiresByType video/mp4                 "access plus 1 year"
ExpiresByType video/webm                "access plus 1 year"

# HTC files  (css3pie)
ExpiresByType text/x-component          "access plus 1 month"

# Webfonts
ExpiresByType application/x-font-ttf    "access plus 1 month"
ExpiresByType font/opentype             "access plus 1 month"
ExpiresByType application/x-font-woff   "access plus 1 month"
ExpiresByType application/x-font-woff2  "access plus 1 month"
ExpiresByType image/svg+xml             "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"

# CSS and JavaScript
ExpiresByType text/css                  "access plus 1 month"
ExpiresByType application/javascript    "access plus 1 month"
</IfModule>
 

RDO Servers

Well-known member
Joined
Apr 3, 2015
Messages
1,026
Best answers
0
Ratings
416 3
Points
83
#6
Browsers use the expires headers to know what resources they should and should not cache, and for how long.

Without this, your users browsers may not cache content for long enough, or possibly not at all. This causes more round-trips to your server, which increases your resource usage and decreases your site performance.
 

Chris Worner

Well-known member
Joined
Apr 15, 2016
Messages
287
Best answers
0
Ratings
25
Points
28
#7
Chris Worner
Good explanation! If I don't add expires header into .htaccess file then what will happen? browsers will not cache my content (images,js,html..)? What is the default setting for caching content from browsers if we don't have expires header?
 
Latest Threads
Recommended Threads

Latest Hosting OffersNew Reviews

Sponsors

Latest Blog ArticlesMost Viewed Threads

Top