Search code examples
asp.net-corefaviconasp.net-core-2.1

Serve favicon.ico from somewhere other than root


ASP.NET Core 2.1.

My favicon is at /images/favicon.ico and cannot be changed. So pages include this:

<link href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />

And that works.

But requests directly to example.com/favicon.ico will fail with a 404.

How do I redirect example.com/favicon.ico to example.com/images/favicon.ico?


Solution

  • In the Configure method of your Startup class, add the following code

    RewriteOptions rewriteOptions = new RewriteOptions().AddRedirect("favicon.ico", "images/favicon.ico");
    
    app.UseRewriter(rewriteOptions);
    

    See this page for more information