0

On my Windows 2012 server I have installed the URL Rewrite module in IIS. I have followed this guide to redirect non-www to www in web.config: http://www.surfingsuccess.com/asp/iis-url-rewrite.html#.VF6GBid0yAU

So far, so good!

The only problem is that we also host the subdomain "api.example.com". This API stops working when I apply the code in web.config.

My question is: What is wrong with the code below? Why does the subdomain stop working when I try to redirect non-www to www, except "api.example.com"?

    <rewrite>
        <rules>
            <rule name="CanonicalHostNameRule1">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^api\.example\.com$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://www.example.com/{R:1}" />
            </rule>
        </rules>
    </rewrite>
Øyvind
  • 11
  • 2

2 Answers2

0

I have similar check and I have same rule but have specified differently in pattern.

My suggestion is to enable Failed request tracing to check URL rewrites. http://www.iis.net/learn/extensions/url-rewrite-module/using-failed-request-tracing-to-trace-rewrite-rules

Edit: Updated rule.

 <rewrite>
            <rules>
                <rule name="non-root" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_HOST}" pattern="^(www.example.com)$" negate="true" />
                        <add input="{HTTP_HOST}" pattern="^(api.example.com)$" negate="true" />
                        <add input="{HTTP_HOST}" pattern="^(example.com)$" />
                    </conditions>
                    <action type="Redirect" url="http://www.example.com/{R:1}" />
                </rule>
            </rules>
        </rewrite>
bdoshi
  • 1,001
  • 12
  • 19
  • Unfortunatly that did not help either, api.example.com still stop working when I add the code in web.config (example.com). I think this is pretty strange since api.example.com is one site in IIS, and example.com is another site in IIS. When I user url rewrite on one site (example.com), why does that effect another site (api.example.com). They also run in separate application pools. – Øyvind Nov 13 '14 at 11:11
  • Is it possible to make a rule that only redirects to www.example.com if the input doesn't start with www. or api.? – Øyvind Nov 13 '14 at 12:06
  • Are you adding the Rewrite rule at WWWROOT\web.config level? Do you have anything else that could be interfering? I suggest to try Failed Request Tracing to see the log. It will pinpoint exactly where the issue is. – bdoshi Nov 13 '14 at 15:10
  • @Øyvind updated answer. This is working on my local IIS web.config at wwwroot – bdoshi Nov 13 '14 at 15:30
  • Actually, there was nothing wrong with my rewrite code. The reason why the subdomain stopped working was because it had a referance to the top domain of the site (example.com), this caused a conflict with the rewrite code. After changing the referance to the www -version of the doman (www.example.com) everything worked fine. – Øyvind Nov 21 '14 at 10:18
0

Actually, there was nothing wrong with my rewrite code. The reason why the API stopped working was because it had a referance to the top domain of the site (example.com), this caused a conflict with the rewrite code. After changing the referance to the www -version of the doman (www.example.com) everything worked fine.

Øyvind
  • 11
  • 2
  • @yvind, had a question, you mentioned you had a "reference" to the top domain of the site, where was that reference. I am having this same issue. I have not officially tried your code yet, but are you saying your rewrite code works? I am just wondering where this "referenece" to the top domain was in case I have the same issue. – Alpdog14 Jan 31 '18 at 16:09