What is the best way to set far future expires on images with Django?

django

Solution

With `django-compress`, you're treating CSS and JS as dynamic files - which is fine. However, the other files are static - and Django doesn't really want to serve these. They should be served directly by Apache or other webserver.

With Apache, you'd put something like

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
</FilesMatch>

in `.htaccess` or the global Apache configuration.

Update: Responding to your comment - Sorry I didn't understand the nuance of your question. There isn't much you can do to avoid using a different URL to indicate to a client that a file (e.g. an image) has changed. This is recommended by e.g. Apache and YAHOO!

Implementing this is IMO not too hard. It's in two parts:

- Use a tag (here's a useful snippet) to generate versioned URLs to media files which will appear in your HTML. The versioned URL could be the base URL with the version inserted, e.g. `/media/3/header.png` where the actual file remains as `/media/header.png` on the server.

- Use a tool like `mod_rewrite` to convert incoming URLs to the canonical value, i.e. a request for `/media/3/header.png` gets converted to `/media/header.png`. This article describes the feature in more detail.

Problem

I am using django-compress with far future expires for my css and js files. Works great. I'd like to do something similar for other static content (images, flash, etc). I can set a far future expires on this content, but I must manually rename the files when they change. Is there a better way to handle this?

Original source