Share via


Redirect HTTP to HTTPS in Azure (With Load Balancer)

Question

Wednesday, October 12, 2016 1:02 AM

We have 2 Web servers in Azure that are Load balanced. We just installed SSL in our these azure websites to convert it to HTTPS.

Now we want that any request coming in as HTTP should be changed/redirected to HTTPS connection.

So, I for testing I created a published website on my local machine, then added self signed SSL certificate to get a secure site. Then I used URL rewrite to direct my HTTP site to HTTPS. I used this in Web.config.

This works perfectly on my local published site.

But this fails on the Azure server and gives me an Internal Server Error.

Any ideas?

I used the following in Web.config for the URL rewrite:

<rewrite>
    <rules>     
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
            <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="Off" />
                    <add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="SeeOther" />
        </rule>         
    </rules>
</rewrite>

All replies (4)

Thursday, October 13, 2016 8:38 AM ✅Answered | 2 votes

Hi,

I am very happy to answer the questions.

I think we can use URL Rewrite to redirect HTTP to HTTPS. We can follow these steps to achieve this function.

1.If you don’t have URL Rewrite, we can download it.

https://www.microsoft.com/en-US/download/details.aspx?id=7435

When you install it successfully, you can see URL Rewrite in IIS.

2. The site is bound by the following two protocols:443 80

3.Configuring the URL Rewrite.

4.We test it.

In Azure VM, we access http://10.0.0.5 , it will redirect to https://10.0.0.5 successfully.

Now, we should pay attention to configure in Azure.

1. Web Server NSG rules.

For Input rules, 80 and 443 should be allowed to access.

2.Load Balance Set.

I think we should set two load balance set, 80 and 443.

Maybe application gateway can also achieve this function.

If you still have any problems, you can contract me. Thanks

Regards,

Walter

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have feedback for TechNet Subscriber Support, contact [email protected].


Wednesday, October 12, 2016 10:01 AM

Hello Shantnu,

Thank you for posting on the Azure forums!

There might be a slight modification required on this code to work on Azure. Refer to https://social.msdn.microsoft.com/Forums/azure/en-US/de6a67ae-6679-41d2-af4e-9ac264a404d0/how-to-autoforward-http-traffic-to-https-basically-enforce-ssl?forum=windowsazuredevelopment which discusses a similar issue and a great article by one of our customers can help guide you in this regards.

Hope this helps.

Regards,

Loydon

________________________________________________________________________________________________________________

When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer so that other customers can benefit from it.


Wednesday, October 12, 2016 8:21 PM

This did not work. My site is not MVC. Its a traditional C# .NET website on IIS. And I have 2 load balanced VMs on Azure hosting the website. 

Any ideas ?


Friday, October 14, 2016 4:42 PM

Hey Walter, 

Thanks for the detailed explanation.

This is Perfect and worked very well. 

I was missing the URL rewrite module on IIS on the server. Installing that did the trick

https://www.iis.net/downloads/microsoft/url-rewrite

Now the IIS server can redirect the url requests from HTTP to HTTPS :-)