问题
My favicon is at /images/favicon.ico
rather than the root. In Startup.cs
I can added a redirect to avoid a 404:
app.UseRewriter(new RewriteOptions()
.AddRedirect("favicon.ico", "images/favicon.ico"));
That works nicely, but of course relies on a redirect. The docs also show how to use rewriting, so the user doesn't see a redirect.
I tried this, but get a 404:
app.UseRewriter(new RewriteOptions()
.AddRewrite("favicon.ico", "images/favicon.ico", skipRemainingRules:true));
So I thought that maybe I need to use a dummy regex:
app.UseRewriter(new RewriteOptions()
.AddRewrite("^(favicon.ico)$", "images/favicon.ico", skipRemainingRules:true));
But I still get a 404. How do I make this work?
回答1:
As per Ionix's answer, the rewrite rules must be applied before the call to UseStaticFiles()
Old answer, no longer relevant
Following your question I've done a lot of testing with the AddRewrite
method and I've tried a lot of things inspired by the official documentation but nothing worked. At best I got a redirect ...
So my answer is: you can't ! Only a redirect works. If you find a way to make a true rewrite work, please let me know but for now I'll consider this is impossible.
Why not simply use the working solution, the redirect ? Do you need a rewrite ?
来源:https://stackoverflow.com/questions/51787969/url-rewriting-in-asp-net-core-2-1-without-regex