Search code examples
pythondjango.htaccesscache-control

Cache-Control only working for media files and not static files served using django-pipeline in Django


I'm trying to use Cache-Control for my static files. Following is my code for the .htaccess file

# 1 YEAR
<FilesMatch "\.(ico|svg|woff|eot|ttf)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 WEEK
<FilesMatch "\.(jpg|png|gif|css|js)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

I'm using Django-Pipeline to compress my JS and CSS files. Following is my relevant settings.py

MEDIA_ROOT = '/home/jaskaran/edmhunters/media'
MEDIA_URL = '/media/'

STATIC_ROOT = '/home/jaskaran/edmhunters/static'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    root('hunt/static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATICFILES_STORAGE = 'pipeline.storage.PipelineCachedStorage'

Testing a media file URL using redbot.org gives something like this

HTTP/1.1 200 OK
    Date: Sat, 13 Sep 2014 08:46:35 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Wed, 10 Sep 2014 17:57:24 GMT
    ETag: "4b6d-502b9c8c3966e"
    Accept-Ranges: bytes
    Content-Length: 19309
    Cache-Control: max-age=604800, public
    Keep-Alive: timeout=5, max=100
    Connection: Keep-Alive
    Content-Type: image/jpeg

For a static file this is what it outputs

HTTP/1.1 200 OK
    Date: Sat, 13 Sep 2014 08:49:12 GMT
    Server: Apache/2.4.7 (Ubuntu)
    Last-Modified: Tue, 26 Aug 2014 05:43:32 GMT
    ETag: 1409031812.69
    Content-Length: 23907
    Keep-Alive: timeout=5, max=99
    Connection: Keep-Alive
    Content-Type: image/png

Any idea what i'm missing?


Solution

  • You are serving static files from django (through django-pipeline) but from web server. Then .htaccess directives have no effect.