Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core redirect http to https

I have created a redirect rules in my web.config to redirect my website from http to https. The issue i have is that every single link on the website is now https. I have a lots of link to other website which don't have SSL and therefore i get certificate errors. This is what i have done:

  <rewrite>
  <rules>
    <rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

How can i redirect https only for my domain and not every links on my website?

like image 677
Huby03 Avatar asked Apr 10 '17 09:04

Huby03


People also ask

How do I redirect http to HTTPS in dotnet core?

Require HTTPS. We recommend that production ASP.NET Core web apps use: HTTPS Redirection Middleware (UseHttpsRedirection) to redirect HTTP requests to HTTPS. HSTS Middleware (UseHsts) to send HTTP Strict Transport Security Protocol (HSTS) headers to clients.


2 Answers

Edit: While it still works, things changed a bit since I wrote this answer. Please check D.L.MAN's more up to date answer: https://stackoverflow.com/a/56800707/844207

In asp.net Core 2 you can use an URL Rewrite independent of the Web Server, by using app.UseRewriter in Startup.Configure, like this:

        if (env.IsDevelopment())
        {
            app.UseBrowserLink();
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");

            // todo: replace with app.UseHsts(); once the feature will be stable
            app.UseRewriter(new RewriteOptions().AddRedirectToHttps(StatusCodes.Status301MovedPermanently, 443));
        }
like image 90
victorvartan Avatar answered Oct 08 '22 05:10

victorvartan


Actually (ASP.NET Core 1.1) there is a middleware named Rewrite that includes a rule for what you are trying to do.

You can use it on Startup.cs like this:

var options = new RewriteOptions()
    .AddRedirectToHttpsPermanent();

app.UseRewriter(options);
like image 30
Sergio López Avatar answered Oct 08 '22 05:10

Sergio López