aspnet / BasicMiddleware

[Archived] Basic middleware components for ASP.NET Core. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
169 stars 84 forks source link

URL rewrite doesn't work for non-regex #355

Closed mkArtakMSFT closed 6 years ago

mkArtakMSFT commented 6 years ago

From @lonix1 on August 10, 2018 16:10

ASP.NET Core 2.1 (2.1.302)

I want to rewrite /favicon.ico to /images/favico.ico.

So I followed the docs and did this in Startup.cs at the end of Configure() (after app.UseMvc(), I hope that's correct?):

app.UseRewriter(new RewriteOptions()
  .AddRewrite("favicon.ico", "images/favicon.ico", skipRemainingRules:true));

But that gave me a 404.

So I thought maybe I need to use a dummy regex:

app.UseRewriter(new RewriteOptions()
  .AddRewrite("^(favicon.ico)$", "images/favicon.ico", skipRemainingRules:true));

And that also gave me a 404.

This was confirmed by other people on SO, so it's either a bug, or the documentation is wrong (in which case please correct me).

Copied from original issue: aspnet/Mvc#8254

mkArtakMSFT commented 6 years ago

Thanks for contacting us, @lonix1. Please provide us with a minimalistic repro project so we can investigate the issue.

mkArtakMSFT commented 6 years ago

From @kichalla on August 10, 2018 18:30

cc @jkotalik

mkArtakMSFT commented 6 years ago

From @jkotalik on August 10, 2018 18:32

Seems like a bug, will look into it in the 2.2 timeframe.

mkArtakMSFT commented 6 years ago

@jkotalik, should this be moved to a different repo?

mkArtakMSFT commented 6 years ago

From @jkotalik on August 10, 2018 23:16

Oops: just BasicMiddleware: https://github.com/aspnet/BasicMiddleware

mkArtakMSFT commented 6 years ago

From @gsdnano on August 11, 2018 3:28

This can be reproduced by taking the example at https://docs.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-2.1&tabs=aspnetcore2x and commenting out the app.Run command at the end of the Configure function. The url rewrite example will then 404 no matter the replacement url. This is using core 2.1 and Microsoft.AspNetCore.Rewrite 2.1.1.

mkArtakMSFT commented 6 years ago

From @lonix1 on August 11, 2018 6:0

Once this bug is resolved, please also give us some guidance (or better yet, update the docs) about the implications of putting this before/after/between app.UseStaticFiles() and app.UseMvc(), or whether it should be at the beginning/end of Configure() i.e. the beginning/end of the pipeline.

mkArtakMSFT commented 6 years ago

@muratg FYI

Tratcher commented 6 years ago

So I followed the docs and did this in Startup.cs at the end of Configure() (after app.UseMvc(), I hope that's correct?):

Rewrite must come early in the pipeline, not at the end. In your case it needs to come before UseStaticFiles.

jkotalik commented 6 years ago

@lonix1 I assume this solved your issue. Closing. You may reopen if you are still having issues.