Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Having trouble setting expires headers and so on...


Go to End


2 Posts   2861 Views

Avatar
Spaghetti

Community Member, 32 Posts

10 January 2014 at 1:37am

I've been trying to speed up my site and various speed measuring sites have pointed out that my main priority should be to set the expires header on the served content to 'Leverage browser caching'. I thought I could do this by making use of mod_expires, but for some reason it's not working.

Also I've been advised to add 'Header append Vary: Accept-Encoding', because apparently some clients can't handle decryption (fair enough....?!). So I looked at adding that via mod_headers.

Both of those Apache modules are installed on my VPS but after testing with those page speed tools and double-checking the headers with a tool called rebot, http://redbot.org/, I can see it's not working.

What am I doing wrong?

Here's the htaccess script:

### SILVERSTRIPE START ###
<Files *.ss>
	Order deny,allow
	Deny from all
	Allow from 127.0.0.1
</Files>

<Files web.config>
	Order deny,allow
	Deny from all
</Files>

# This denies access to all yml files, since developers might include sensitive
# information in them. See the docs for work-arounds to serve some yaml files
<Files *.yml>
	Order allow,deny
	Deny from all
</Files>

# Define some expiry header settings.
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/html "access plus 5 minutes"
  ExpiresByType image/gif "access plus 7 day"
  ExpiresByType image/png "access plus 7 day"
  ExpiresByType image/jpg "access plus 7 day"
  ExpiresByType image/jpeg "access plus 7 day"
  ExpiresByType image/ico "access plus 7 day"
  ExpiresByType text/css "access plus 7 day"
  ExpiresByType text/javascript "access plus 7 day"
  ExpiresByType application/x-javascript "access plus 7 day"
</IfModule>

# Append the 'Vary: Accept-Encoding' for resources that might need it.
<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html

<IfModule mod_rewrite.c>
	SetEnv HTTP_MOD_REWRITE On
	RewriteEngine On
	RewriteBase '/'


	RewriteRule ^vendor(/|$) - [F,L,NC]
	RewriteRule silverstripe-cache(/|$) - [F,L,NC]
	RewriteRule composer\.(json|lock) - [F,L,NC]
	
	RewriteCond %{REQUEST_URI} ^(.*)$
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_URI} !\.php$
	RewriteRule .* framework/main.php?url=%1&%{QUERY_STRING} [L]
</IfModule>
### SILVERSTRIPE END ###

and here's the output from redbot:

    HTTP/1.1 200 OK
    Server: nginx
    Date: Thu, 09 Jan 2014 12:35:02 GMT
    Content-Type: text/html; charset=utf-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    Cache-Control: no-cache, max-age=0, must-revalidate, no-transform, max-
        age=300
    Expires: Thu, 09 Jan 2014 12:40:02 GMT
    Content-Encoding: gzip

It also says 'This response is negotiated, but doesn't have an appropriate Vary header.' and critically: 'The max-age Cache-Control directive appears more than once.' - is something else inside Silverstripe or anything else setting that expiry time to 0?

tl;dr; Do I have to do anything special in SilverStripe to set an expiry time on files and/or content?

Any help, much appreciated.

Avatar
Spaghetti

Community Member, 32 Posts

14 January 2014 at 3:04am

I've just read the documentation here: http://doc.silverstripe.org/framework/en/howto/cache-control

removed my header manipulation code from the .htaccess file and rechecked using Redbot to see what the default headers are - since I've not touched the site all weekend, I'd expect at the very least for the expires header to be '+2 days' - it isn't, it's 0.

Is this a bug or am I missing something?