Prevent caching of XML files with Apache .htaccess rules

Preventing web cache of XML file with htaccessI am having problems with a dynamically created images.xml being cached in internet explorer. The xml file is used by a flash slideshow player and for different pages on the site, has different images to show. IE isn’t reading the new images.xml each time.

I had a go at adding some cache control code to my .htaccess file but got an internal server error.

Code:

[cc escaped=”true” lang=”text”]RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|tinymce|flash|filemanager|sifr|scripts|ssi|slideshow|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

<FilesMatch “\.(xml)$”>
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 1990 05:00:00 GMT”
</FilesMatch>[/cc]

Than I tried replacing this
RewriteCond $1 !^(index\.php|images|css|tinymce|flash|filemanager|sifr|scripts|ssi|slideshow| robots\.txt)

With:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

 

Add Cache-Control Headers

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 add Cache-Control Headers to certain files.
[cc escaped=”true” lang=”text”]# 480 weeks
<FilesMatch “\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$”>
Header set Cache-Control “max-age=290000000, public”
</FilesMatch>

# 2 DAYS
<FilesMatch “\.(xml|txt)$”>
Header set Cache-Control “max-age=172800, public, must-revalidate”
</FilesMatch>

# 2 HOURS
<FilesMatch “\.(html|htm)$”>
Header set Cache-Control “max-age=7200, must-revalidate”
</FilesMatch>[/cc]
If you are using far Future Expires Headers and Cache-Control (recommended), you can do this for these files.
[cc escaped=”true” lang=”text”]<FilesMatch “\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$”>
Header set Cache-Control “public”
Header set Expires “Thu, 15 Aug 2012 20:00:00 GMT”
</FilesMatch>[/cc]

Without Cache-Control Headers

No Cache-Control Headers

No Cache-Control Headers

With Cache-Control Headers

Cache-Control Headers On

Cache-Control Headers On

Currently AskApache.com Uses

You can use the HTTP Header Viewer Tool to check it.
[cc escaped=”true” lang=”text”]Header unset Pragma
FileETag None
Header unset ETag

# 1 YEAR
<FilesMatch “\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$”>
Header set Cache-Control “public”
Header set Expires “Thu, 15 Aug 2012 20:00:00 GMT”
Header unset Last-Modified
</FilesMatch>

# 2 HOURS
<FilesMatch “\.(html|htm|xml|txt|xsl)$”>
Header set Cache-Control “max-age=7200, must-revalidate”
</FilesMatch>

# CACHED FOREVER
# MOD_REWRITE TO RENAME EVERY CHANGE
<FilesMatch “\.(js|css)$”>
Header set Cache-Control “public”
Header set Expires “Thu, 15 Apr 2010 20:00:00 GMT”
Header unset Last-Modified
</FilesMatch>[/cc]

It's only fair to share...Buffer this pageShare on Facebook0Tweet about this on TwitterShare on Google+0Share on LinkedIn0Share on Reddit0Pin on Pinterest0Email this to someone
About

Just a guy with strong interest in PHP and Web technologies

Posted in Apache, Servers, Small Tips Tagged with: , , , , , , , , , , , ,
0 comments on “Prevent caching of XML files with Apache .htaccess rules
1 Pings/Trackbacks for "Prevent caching of XML files with Apache .htaccess rules"
  1. […] the general case the website caching happens on the server side where the PHP output or database objects are cached in static files or […]

Leave a Reply

Your email address will not be published. Required fields are marked *

*