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

Single Redirect with Rewrite Middleware #262

Closed RehanSaeed closed 6 years ago

RehanSaeed commented 7 years ago

When using multiple rules with the rewrite middleware like so:

app.UseRewriter(
    new RewriteOptions()
    .AddRedirectToHttps()
    .AddRedirect("(.*[^/])$", "$1/"));

When I pass a URL that is both HTTP and has upper case characters, you get two redirects:

untitled

The first is the redirect from HTTP to HTTPS and the second is the redirect to the lower-case URL (not sure why Chrome dropped /about in the URL but it's there in the address bar). Ideally from a performance perspective, it should be possible to run all URL rewrite rules and apply a single redirect to the resultant URL. Is that currently possible? If not, what would it take to make it so?

Tratcher commented 7 years ago

This is not available for redirect rules. All effective redirect rules are considered terminal and no further rules are run. Does this work in other rewrite systems?

RehanSaeed commented 7 years ago

This seems to be called 'redirect chaining'. This blog post talks about how you can carry out a single redirect with IIS and avoid chaining. One other advantage to reducing chaining that I hadn't considered is that for every redirect you carry out, Google passes on less link juice, so it also hurts SEO.

Tratcher commented 7 years ago

Clever. That should mostly work for the current rule system. However, there's no built in rewrite version of the https rule, only a redirect version. It wouldn't be hard for you to make one though.

jkotalik commented 7 years ago

I haven't managed to test those UrlRewrite rules. but you may be able to copy it over and use it. However, we should consider giving an option that allows you to apply all rules and redirect afterwards on code rules.

RehanSaeed commented 7 years ago

My use case is a little more complicated than using the linked regex rules, I'd like to use a rewrite rule instead using code which also has the added benefit of being faster. Implementing some kind of pipeline so that the URL can be transformed by a sequence of rules would be useful.

Tratcher commented 7 years ago

The architecture would be pretty strange. You'd have to do a series of rewrite rules that set a flag if they changed anything, and then a final redirect rule that redirects to the resulting 'current' url if the flag is set. To build this in you'd need to define a new class of delayed redirect rules.

RehanSaeed commented 6 years ago

2.1 has added HttpsRedirectionMiddleware.

It would be nice for AddRedirectToHttps to also support automatically setting the SSL port generally and in particular if a single redirect was implemented.

muratg commented 6 years ago

We don't think we'll be doing a large redesign here. Code rules are terminal, and this would require a redesign.