Answer the question
In order to leave comments, you need to log in
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);
file.php?path=/my/file.svg
Answer the question
In order to leave comments, you need to log in
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']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question