web.config redirects only for specific domain
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.
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>
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
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

Comments
Post a Comment