« Speed Tips: Turn Off ETagsSpeed Up Google Analytics with urchin.js »
Speed Tips: Remove Last-Modified Header
September 10th, 2007
If you remove the Last-Modified and ETag header, you will totally eliminate If-Modified-Since and If-None-Match requests and their 304 Not Modified Responses, so a file will stay cached without checking for updates until the Expires header indicates new content is available!
Easy Apache Speed Tips Articles
- Turn On Compression
- Add Future Expires Header
- Add Cache-Control Headers
- Turn Off ETags
- Remove Last-Modified Header
- Use Multiple SubDomains
Please don't turn off ETags and Last-Modified headers for your .html files, leave one of them ON. (I use Last-Modified for .html).
Remove Last-Modified Header
This goes in your root .htaccess file but if you have access to httpd.conf that is better.
This code uses the FilesMatch directive and the Header directive to remove all Last-Modified Headers from being sent.
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$"> Header unset Last-Modified </filesMatch>
How it Works
By removing both the ETag header and the Last-Modified headers from your static files (images, javascript, css) browsers and caches will not be able to validate the cached version of the file vs. the real version. By also including a Cache-Control header and Expires header, you can specify that certain files be cached for a certain period of time, and you magically (this is a really unique trick I promise) eliminate any validation requests!!
Reader Comments
-
Does this work? I've removed both the Etag and Last-Modified headers, and added an expires header, but it always revalidates with 200 response. Can someone who's got this working post a URL please? Thanks
-
I found this tip valuable until today... when I realize that it is not complete. Removing the ETag header is ok (each server generate a different ETag, in a cluster/farm this will be a problem, not a solution). But by removing the last-modified header you will make the browser use the "Expires" only, when the content Expires on browser cache it (the browser) will fetch a new content, regardless if is modified or not. If you use Expires and last-modified together, the browser (firefox on my test, YMMV) use the Expires, and after that it start to use last-modified getting the 304 (not modified) as a benefit. This way you get a two layer of cache: 1- absolutely local, 2- Browser e Server if-modified-since/not-modified handshake; Klaubert
-
Damn.... I am still undecided what to do with the last modified date and ETags. I have a project in hand that needs me to set last modified date and Etags. Its an average site that is a reseller webhosting company. So there would be quite a bit of return traffic. What do you suggest in this case? keep it or remove it? I am seriously not able to decide....HELP!!!!
-
Having some issues with pagespeed (aka google's analysis tool). It's saying I need to specify a last-modified header, but the htaccess file has the following:
ErrorDocument 401 http://www.site.co.uk/401-page.html ErrorDocument 404 http://www.site.co.uk/404-page.html ErrorDocument 500 http://www.site.co.uk/500-page.html RewriteEngine On RewriteCond %{HTTP_HOST} ^site\.co.uk [nc] RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L] ExpiresActive On ExpiresByType image/ico A29030400 ExpiresByType image/gif A2592000 ExpiresByType image/png A2592000 ExpiresByType image/jpg A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType text/javascript A2592000 ExpiresByType text/css A2592000 ExpiresByType text/swf A29030400 ExpiresByType text/js A29030400 Header add X-Enabled mod_gzip Header add X-Enabled mod_deflate Header set Cache-Control "private" Header set Expires "Thu, 27 Jul 2011 10:00:00 GMT" FileETag none Header unset Last-ModifiedIs the file correct - are there clashes/ etc. Current pagespeed score is 91% and yslow gives it a Grade A. Before I move on to learning about CDN's, I need to make sure I'm on the right track with htaccess files!
-
Thanks. RTT time is a big part of little page loading time. i never thought about remove these two headers, but it really works, at least one round request and response saved.
Pingback:Google’s Need for Speed – Use Cache in .htaccess to Speed Up Your Site — Chicago Style SEO
-
Information isn't valid. Existence of Last-Modified / ETag doesn't influence rate of cache integtiry checks in browsers
-
I love this tip. I've done a lot of optimization research and actually nobody ever suggested to remove the Last-Modified header and it's interesting to know that the browser will not pass in those "If-" requests which is once again less bytes that have to travel across the web. My only question is why are you suggesting NOT to remove the Last-Modified header for .html files?
-
This is what I currently have in my .htaccess file at the root. Do you see any problems with what I have here?
First off, once your rules are correct be sure to move the rules out of .htaccess and into the httpd.conf file for the Directory section of your DocumentRoot. Why? .htaccess is inherently slow as it must be read for each and every request that starts in or goes deeper than the directory in which it resides (and for which AllowOverride is enabled). See the Apache documentation for .htaccess files for more information -
This is what I currently have in my .htaccess file at the root. Do you see any problems with what I have here? Would you organize it differently? I don't know much about .htaccess files, but would love to see if this is ok for my server. Here it is below...
FileETag None Header unset ETag Header 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" Header unset ETag FileETag None SetOutputFilter DEFLATE Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT" Header unset Last-Modified
Thanks for your help! -
BTW...auto-playing music is a good way to lose traffic. Is this a MySpace page?
-
Yes, this will speed individual requests, but for those sites with actual traffic they may crawl to a halt due to re-requested content from frequent visitors (think RSS feeds) and spiderbots (Google, etc.). It's a trade-off that massive scalable systems must consider: slow each request a little to be able to serve all requests reasonably fast.
