Sorry for the vague title, the problem is too complex to summarize in a short phrase…
I’m trying to set up the following redirection rules:
blog.mydomain.net/en/something: redirected towww.mydomain.com/somethingblog.mydomain.net/fr/something: redirected towww.mydomain.fr/somethingblog.mydomain.net/*: redirected towww.mydomain.com
Rule 3 is working, but rules 1 and 2 seem to be skipped so rule 3 is always applied. Here are my web.config rules:
<!-- Canonicalize mydomain.com to www.mydomain.com -->
<rule name="CanonicalHostNameRule_en" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Canonicalize mydomain.fr to www.mydomain.fr -->
<rule name="CanonicalHostNameRule_fr" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^mydomain\.fr$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.fr/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/en/something to www.mydomain.com/something -->
<rule name="RedirectBlog_en" enabled="true" stopProcessing="true">
<match url="^/en(/.*)?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/fr/something to www.mydomain.fr/something -->
<rule name="RedirectBlog_fr" enabled="true" stopProcessing="true">
<match url="^/fr(/.*)?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.fr/{R:1}" />
</rule>
<!-- Redirect blog.mydomain.net/* to www.mydomain.com -->
<rule name="RedirectBlog_other" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^blog\.mydomain\.net$" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/" />
</rule>
<!-- WordPress-specific rules -->
...
I don’t understand why rules RedirectBlog_en and RedirectBlog_fr are skipped; I tested the regular expressions and they work fine.
Can anyone spot the problem?
EDIT: if I disable the 3rd rule (RedirectBlog_other), then rules 1 and 2 work fine… how is it possible, since rules 1 and 2 are executed before rule 3?
OK, I got it!
First, things weren’t happening as I thought; rules 1 and 2 were not working when I disabled rule 3: I was still redirected to my actual domain, but this was done by WordPress, not by my rules.
Second, my pattern for matching the URL was wrong: the leading ‘/’ is not included in the input, so my rules weren’t matching at all.