E
E
Extramezz2016-08-08 16:54:13
PHP
Extramezz, 2016-08-08 16:54:13

Redirect caching?

Our project uses a boxed nginx server and it so happened that there is no way to enable caching of "whatever we want" files. Many files already have it enabled by standard settings (css, js, png, jpeg, etc.), but SVG files ... for them - no.
Something interesting came to mind. Write a script like this:

header('Cache-Control: max-age=604800');
header('Location: ' . $_GET['path'], true, 301);

Well, send requests in style
file.php?path=/my/file.svg
Question: What will the browser cache in this case? The fact of a redirect to a file or also the svg itself?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vamp, 2016-08-08
@Extramezz

All headers refer exclusively to their response. After receiving the redirect, the browser will generate a new separate request to the resource /my/file.svg and the server will return its headers related only to this resource, and there will be no caching directive in these headers.
So in your case the redirect will be cached. That is, the browser will remember that the url "file.php?path=/my/file.svg" redirects to "/my/file.svg" (in principle, it will remember this even without additional Cache-Control) and if it encounters somewhere on the first url on the page, it will send a request immediately to the second url, bypassing the request to the first one.
You need to give the image content directly to the script itself:

header('Content-Type: image/svg+xml');
// public явным образом разрешает кешировать контент не только на
// устройстве пользователя, но и на любом промежуточном кеширующем http сервере,
// если такой будет стоять между вами и пользователем
// например, провайдерский или в офисах
header('Cache-Control: public, max-age=604800');

// ОСТОРОЖНО!!! Сначала произведите валидацию
// параметра path перед передачей его в readfile()!
readfile($_GET['path']);

It shouldn't be resource intensive. Still cached for a week.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question