What is the optimal way to remove the www subdomain from a url using IIS URL Rewrite?
Asked
Active
Viewed 2.0k times
4 Answers
42
If you want it to work with any hostname (not hardcoding it into the rule), you'd want to do something like this:
<rule name="Remove www" stopProcessing="true">
<match url="(.*)" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
in the redirect action, the {C:1} contains the second capturing group in the condition, whereas the {R:0} contains whatever was in the rule (the path). appendQueryString="true" will also append any querystring to the redirect (if present). Keep in mind though, that any url hashes, if present, will be lost in the process since those don't get passed to the server.

Jani Hyytiäinen
- 5,293
- 36
- 45
-
I was never thrilled with the hard coded fragment in the first answer, glad i can finally mark an answer on this question. – Chris Marisic Feb 18 '14 at 13:53
-
3What if someone comes in on https? You don't want to just automatically redirect them to http, do you? – jedmao Mar 19 '14 at 22:49
-
Maybe you can just replace `http://` with `//` and it will work the way you want. – jedmao May 30 '14 at 18:43
-
@mrjedmao no, you cannot use protocol relative urls as the base url. The rewrite will rewrite the requests, not the markup. When browser sees href="//google.com" in the markup, it will look at the base url (the one in address bar) and use the same protocol. The request itself will have http:// or https:// in it respectively. Try typing //google.com to your browser. You can modify the markup too with IIS Rewrite, but that'd require an output rewrite rule. – Jani Hyytiäinen Jul 02 '14 at 17:34
-
3Mads Kristensen has a good config example for this which is also protocol agnostic (supports HTTPS): http://madskristensen.net/post/url-rewrite-and-the-www-subdomain – BrutalDev Oct 17 '14 at 19:22
13
IIS does it automatically for you:
Select site > URL rewrite > new rule > Canonical Host Name :)

frapeti
- 1,090
- 13
- 18
5
The following one should work :
<system.webServer>
<rewrite>
<rules>
<rule name="Remove WWW" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://www.example.com{PATH_INFO}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

Dave Hogan
- 3,201
- 6
- 29
- 54

tugberk
- 57,477
- 67
- 243
- 335
-1
To do a redirect that will work for both http and https the following can be used
<rewrite>
<rules>
<rule name="Lose the www" enabled="true" stopProcessing="true">
<match url="(.*)" ignoreCase="true"/>
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^www\.(.*)$"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="{SchemeMap:{HTTPS}}://{C:1}/{R:1}" appendQueryString="true" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="SchemeMap">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>

Martin Brown
- 24,692
- 14
- 77
- 122