Issue with routing

Let’s say I want to route the /favicon.ico request to /assets/icons/favicon.ico in order to keep a tidy root folder. What is the best way I would go about doing that?

I tried adding to my config file:

	'routes' =>[
		[
			'pattern' => '/favicon.ico',
			'action'  => function() {
				go('/assets/favicons/favicon.ico');
			}
		],
	],

And I tried (taking inspiration from the sitemap.xml cookbook recipe) something like:

	'routes' =>[
		[
			'pattern' => '/favicon.ico',
			'action'  => function() {
				return new Kirby\Cms\Response(file('/assets/favicons/favicon.ico'), 'image/x-icon');
			}
		],
	],

Any ideas? I feel I’m close, but not quite there yet… :smiling_face_with_tear:
Thanks in advance!

I cannot see anything wrong in your code, and indeed when trying I can replicate this issue. The first route example you posted works perfectly fine with patterns '/favicon' or '/favicon.jpg', but not with '/favicon.ico' (the / prefix is optional, as far as I know, but the outcome is the same).

In a similar situation a user once had success with putting the route into a plugin rather than into config.php, but at least for me that does not solve the problem at hand; I tried with a plugin and get the exact same outcome.

It almost feels like the router chokes on the .ico being part of the route (as I get a 404 error but no error page, as I do for any other URL that does not exist). Or the server configuration deals with .ico URLs somehow differently – I do not get any error messages in my server logs, though, and there are no .ico-specific rules in my server setup files either (Nginx/Linux).

Mysterious!

PS. You could of course bypass this specific problem by adding a rewrite rule to your .htaccess instead (if you’re on Apache).

1 Like

That was really helpful, in many ways! I still don’t have a solution for my local environment (Laravel Valet which has Nginx) but I uploaded the first solution to my server and it works! :star_struck:

So your guess is probably correct, something about Nginx and .ico makes it go haywire. Now, I would like to see what’s wrong exactly, because inconsistencies between dev and prod are a cause for concern, but at least it works.

Thanks so much!

I’m experiencing the same issue getting a 404 with Valet (actually even without a route). No issue when switching to Mamp.

I wouldn’t use a redirect (which gives you a 302), though, but return the file from the route. This seems to work fine:

	'routes' =>[
		[
			'pattern' => 'favicon.ico',
			'action'  => function() {
				return F::read(kirby()->root('assets') .'/favicons/favicon.ico');
			}
		],
	],
1 Like

Great to hear that my “thinking aloud” already helped :smiley:

And, indeed, this quite obviously has to do with Valet. In my Valet-Linux setup, the file /etc/nginx/sites-available/valet.conf contains this line:

location = /favicon.ico { access_log off; log_not_found off; }

Now I’m not exactly an expert on Nginx configurations, but that seems to be the culprit?! If I read that line correctly, the request for the /favicon.ico never even reaches PHP, or Kirby, for that matter.

1 Like

Brilliant, thank you both so much!