0

I came across a strange problem today when updating .htaccess to update my site.

Like most sites, when a user requests a resource, it's fed through a script for processing. In my case, I have everything run through one PHP script.

I'm trying to make it where if a favicon for any device is requested (including the png icons for phones), I want the caching to be immutable (where the browser never requests the server for the icon again).

In PHP, I have this code for the icons because it generates the png icons:

apache_setenv('isico','yes');

I also have a static favicon.ico file.

This is the part in .htaccess when active causes the entire website to return a 500 internal server error:

<FilesMatch "^(favicon\.ico)$">
    SetEnv isico yes
</FilesMatch>
Header set Cache-Control public,max-age=77777777,immutable env=isico

I know an answer can be to create separate icon files as static files, but I have a system already setup to generate the same icon at different sizes on the fly through the script.

But other than that, is there another way I can fix the above .htaccess code to make it work? I'm using apache 2.4.37.

1
  • You should check your server's error log for the details of the 500 error and include this in your question. Commented Dec 2, 2024 at 2:34

2 Answers 2

1

Since you are using Apache greater than 2.4.10, the following should work:

<IfModule mod_headers.c>
    Header set Cache-Control "public, max-age=77777777, immutable" env=isico:yes
</IfModule>

It validates that it only puts the header if the variable "isico" is set to "yes". Also first checks if the mod_headers module is enabled.

I'm also adding quotation marks for the Cache-Control value to ensure it is interpreted correctly.

Alternatively, since the Environment Variable is apparently not needed, you could try the following more direct approach:

<IfModule mod_headers.c>
    <FilesMatch "^favicon\.ico$">
        Header set Cache-Control "public, max-age=77777777, immutable"
    </FilesMatch>
</IfModule>
3
  • The same issue came back after making your suggested change. Commented Dec 1, 2024 at 21:31
  • I've added a more direct approach that might help you Commented Dec 2, 2024 at 2:18
  • You could expand it, for example to all ico files by using FilesMatch "\.ico$" instead of FilesMatch "^favicon\.ico$" Commented Dec 2, 2024 at 2:20
1

In PHP, I have this code for the icons because it generates the png icons:

Something is missing from your description or your solution. The request (and specifically the FilesMatch directive) is processed by Apache BEFORE handing this over to PHP. If your PHP is acting as a proxy, that's not going to help either - environment variables are NOT passed in http calls.

If PHP is already in the chain for resolving this request, I would put the caching instructions in PHP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.