1

I'm stuck with redirecting for specific domains. The setup consists of azure webapps which bring their own domain *.azurewebsites.net, I want to exclude those from redirecting. For additional domains e.g. example.com I need to redirect

  • to https
  • to www
  • and no redirection if I enter the correct address https://www.example.com

Edit: I use deployment slots, so I want the same web.config to work when I swap a slot in production.

When I tried to use more than one rule, unfortunately I get server or redirect errors. Should I learn regex better? Here is my first rule to exclude the "internal" domains, but that is already not working:

<rule name="ignore azure" stopProcessing="true">
            <match url="(.*)" />  
              <conditions>
                <add input="{HTTP_HOST}" pattern="^azurewebsites\.net$" />
                <add input="{HTTPS_HOST}" pattern="^azurewebsites\.net$" />
              </conditions>
            <action type="none" />  
          </rule>

I have read the 30 first posts on google but none of those exclude another domain name.

2

Based on Joses Answer this does what i need: - I can access the azurewebsites domain while still on the deployment slot - the production domain will always be redirected to subdomain with https

 <rule name="HTTPS and Subdomain in Production" stopProcessing="true">
 <match url="(.*)" />
 <conditions>
 <add input="{HTTP_HOST}" pattern="^.*\.azurewebsites\.net$" negate="true" />
 <add input="{HTTP_HOST}" pattern="^www\..*" negate="true" />
 </conditions>
 <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
 </rule>
1

I think the rule you need is this one:

<rule name="HTTPS ignore Azure" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^.*\.azurewebsites\.net$" negate="true" />
        <add input="{HTTPS}" pattern="^OFF$" />
        <add input="{HTTP_HOST}" pattern="^www\..*" negate="true" />
    </conditions>
    <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" />
</rule>

What this rule does is check if the current domain is not in the form *.azurewebsites.net, doesn't start with www. and if it's not requested through HTTPS. If all three "ifs" apply, then it's a custom domain without www trying to be accessed through HTTP, so you send a permanent redirect (301) to the https:// version of the requested domain adding a www as a subdomain.

It should work OK. Give it a try and tell me.

HTH

  • This is pretty much what we do as well. You might also need further rules to ensure that the request is under SSL, and that the host starts with a www. in case either of those aren't true. Remember that rules are matched in order, from top to bottom. – Zhaph - Ben Duguid Jul 9 '18 at 23:06
  • 1
    In fact the rules above ensure that the domain strarts with www. and the request is under HTTPS too after the fact. No need for extra rules for that. That's the reaseon to use stopProcessing="True" in the exact case "lolops" asks. – jalarcon Jul 10 '18 at 6:53
  • You're probably right. – Zhaph - Ben Duguid Jul 10 '18 at 7:01
  • Thanks for the suggestions, Joses code it does not redirect to 'www' subdomain from https://host.com and i still don't know how to fix. Could you give me another hand Jose? Thanks! – lolops Nov 28 '18 at 14:21
0

If you are just trying to force HTTPS that is now a feature of Azure Web Apps without any config changes required.

How to make an Azure App Service HTTPS only

enter image description here

  • Thank you, i already know but this is not useful in my case as it doesn't cover redirecting the main-domain to www, and not redirecting the azurewebsites domain as it is in the deployment slot. – lolops Jul 5 '18 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.