Log in

View Full Version : .htaccess expiry - exclude directory?



gwmbox
03-31-2010, 03:46 AM
Hi guys

I am wanting to add expiry settings for our images, scripts, css etc and that has been done fine enough via


<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 1 year"
ExpiresByType application/x-javascript "access plus 1 year"
</IfModule>

But this applies the expires to everything but I need a couple of directories to not be added to the expiry as images are replaced often. So how do I tell it to exclude image in X directory?

Thanks

traq
03-31-2010, 09:58 PM
a more local .htaccess rule will override rules inherited from parent directories, so put a new .htaccess file in the desired directory, with your desired expiry.

gwmbox
04-08-2010, 01:16 AM
Ok so how do I tell it NOT to expire png's for example in a separate directory?

Do I have the directory specific .htaccess like the above but remove the image expires? Thus since that htaccess file does not say expire it does not expire even though the one above it does?

Sorry a bit confused

traq
04-08-2010, 02:34 AM
no: the parent rule will apply to child directories unless there is a local rule that contradicts it.
so, set a .htaccess rule for a very short expiry to override the year-long rule in the parent directory:

ExpiresByType image/png "access plus 1 seconds"
## etc.
so, basically, you're changing the local rule to force images to expire right after they're served.



Also, if you ever need to force a new version of a particular file that's not set to expire yet, you can add a nonsense query string to its url:

www.example.com/path/to/file.html?v=whatever

the query string isn't being used to pass any values (in fact, you can do this with images or css files as well), but it tricks the browser into thinking it's a different file so it won''t use the cached version. from css-tricks (http://css-tricks.com/snippets/htaccess/set-expires/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+CSS-TricksSnippets+%28CSS-Tricks+Snippet+Feed%29)