0

I have recently switched my asp.net MVC website to https and want all existing traffic to get redirected to https://www.example.com using web.config rewrite.

I have tried various combinations but not yet successful. need to handle following three scenarios :

 
http://
http://www
https://
geekonweb
  • 384
  • 4
  • 14
  • 1
    https://stackoverflow.com/questions/17714732/web-config-redirect-non-www-to-www/17715586#17715586 might help – Satpal Jun 30 '17 at 09:34

2 Answers2

0

Try this it might help you:

 <system.webServer>
    <rewrite>
        <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
        </conditions> 
        <action type="Redirect" redirectType="Permanent" 
             url="https://{HTTP_HOST}/{R:1}" />
</rule>   
    </rules>
</rewrite>
</system.webServer>
Praveen Maurya
  • 296
  • 1
  • 6
0

You can redirect your traffic to use the https using the Global.asax as well.

Method name: Application_BeginRequest

Your code to redirect will look something like this

strProtocol = "https";

if (HttpContext.Current.Request.Url.ToString().ToLower().StartsWith("http:") == true)
{
    Response.Redirect(strProtocol + "://" + strYourHostName + strYourRemainingURL, false);
}

you can keep the protocol and hostname in the web.config and pick from there.

Ali Ashraf
  • 119
  • 1
  • 8