We have a "catchall" site for all hosts that have the wrong "www" in the URL (for some sites we want it, for some we don't, and the wrong version goes to the catchall for permanent redirection). Currently we use ISAPI Rewrite for this, and it works well, adding or removing "www" as necessary, with an exception for host names used for WebDAV (they have "dav" in the initial host name). I am trying to rewrite this simple rule for the new URL Rewrite Module, but it fails. The import of the Apache file was unnecessarily complex and non-working, so I am trying to rewrite it by hand.
Here are my doubts and notes:
- {C:0} vs. {C:1} and {R:0} vs. {R:1} are completely inconsistent in docs and forum posts: does it count from 0 or from 1??? It seems to ve correct to count from 0, and to give a "The expression ... cannot be expanded" error if counting from 1 (as in your numerous docs and examples!) So the example below is from 0, but I am not sure if that's correct.
- Adding "www" is OK, but I can't remove it (keeps looping to same host with www)
So here is the code:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Add www" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" negate="true" pattern="^(www\.)(.+)$" />
<add input="{HTTP_HOST}" negate="true" pattern="^[^.]*dav.*" />
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
<rule name="Remove www" stopProcessing="true">
<match url=".*" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
<add input="{HTTP_HOST}" negate="true" pattern="^[^.]*dav.*" />
</conditions>
<action type="Redirect" url="http://{C:0}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In the documentation I would further find it useful if it were better explained when "http://" should be put at the beginning of the redirection string. right now the examples are without, but my rules tend to fail without.
P.S.: Minor bug report: the GUI can set the "last rule" option only in rewrite mode, not in redirect mode. The interesting thing is that when reverting back to rewrite mode it still keeps the flag in XML. So eiter the flag should be exposed in both modes, or it should be cleared in redirect mode (if not useful), right?