Removing the www from a URL but preserving HTTP/HTTPS

 

The easiest way to do this (I think) is to have two rules, and specify an input condition. I’m sure there is a way to do it by using HTTP/HTTPS as a variable, but in my experience simpler is better.

So something like this:

<rule name="Remove www http" stopProcessing="true">

<match url="(.*)" ignoreCase="true" />

<conditions logicalGrouping="MatchAny" trackAllCaptures="true">

<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />

<add input="{HTTP}" pattern="^http\:\/\/$" />

</conditions>

<action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />

</rule>

<rule name="Remove www https" stopProcessing="true">

<match url="(.*)" />

<conditions logicalGrouping="MatchAny" trackAllCaptures="true">

<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />

<add input="{HTTPS}" pattern="^https\:|/\/$" />

</conditions>

<action type="Redirect" url="https://{C:1}{R:0}" />

</rule>

 

Note that this is untested, but is based from pasting fragments of rules that work, so it should be fine but the syntax/order for the <conditions> might need a bit of tweaking.

Edited: Updated the rule that this discussion was about with a working tested version thanks to @rinidpp.

Posted on Monday, April 18, 2016 9:50 PM |

Like this? Share it!

No comments posted yet.

Post a comment
Please add 2 and 2 and type the answer here:
Remember me?
Ensure the word in this box says 'orange':