3

The whole application has a global expires header set in the .htaccess I have a URL e.g. /current which I need like a different expiry header.

4 Answers 4

3

Another solution could be to use Apache mod_setenvif and mod_headers to conditionally set cache headers.

SetEnvIf Request_URI "^/current$" OVERRIDE_CACHE_HEADERS=TRUE
Header set Cache-Control no-cache env=OVERRIDE_CACHE_HEADERS

I have not tried this specific configuration, but I have a similar one that sets environment variable with a RewriteRule directive and then uses Header to conditionally change Cache-Control directives

2

Use could use <Location> apache directive to define different options (e.g. expire) for specific URL.

2
  • The URL is being rewritten, can I still use Location with this?
    – Tom
    May 10, 2011 at 14:23
  • 1
    Following @AlexD answer, you can simply add the header at the new location
    – jflaflamme
    Jan 17, 2012 at 16:02
1
+50

I'm assuming:

  1. You want all content inside some directory to have a different expire than the rest of the site--not just a few files. For example: Dynamically generated JS files; frequently changing logo and other images files; or often edited CSS that you want very short cache lifetimes for..
  2. Apache is directly serving up that content--it's not a view folder or something for a framework which PHP is including out of.
  3. You have the mod_expires module enabled

.htaccess files are searched for in every directory along the way to the file being served up if they're enabled starting at the docroot. Therefore, the easiest would be to put a new .htaccess file in the directory for which you wish to change the expire limit. If your site is www.foo.com and you want /current to have a different expire header a simple .htaccess file in www.foo.com/current. The file just needs:

# enable expirations with mod_expires
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 2 weeks"
</IfModule>

This being said... a block in your httpd.conf file(s) for the entire server, a virtual host block, or in the docroot's .htaccess would be more elegant and manageable solutions.

2
  • What about if I wanted different rewrite rules to have different expires? e.g. /page vs /event ?
    – Tom
    Jan 23, 2012 at 7:02
  • If /page and /event are different directories, just put a custom .htaccess in each. Also if you want it more centralized, create a <location> block for each in a central .htaccess or httpd.conf file. Location blocks are like if checks on a url, if they match, the configuration lines inside the block are applied, otherwise they're skipped. The way I usually roll is to first get it working with the .htaccess files in the desired directories. Then convert them to location blocks in the docroot .htaccess file (that way you avoid .htaccess file pollution!)
    – Ray
    Jan 23, 2012 at 15:28
0

A useful example that should integrate well with rewrites:
http://mark.koli.ch/2010/12/set-cache-control-and-expires-headers-on-a-redirect-with-mod-rewrite.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .