Avatar of webressurs
webressursFlag for Norway

asked on 

Redirect Non-WWW to WWW in Web.config (Except one spesific subdomain)

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.mysite.com". This API stops working when I apply the <rewrite> code in web.config. My question is: How can I change the <rewrite> code below to redirect non-www to www, except for the subdomain "api.mysite.com"?

    <rewrite>
      <rules>
        <rule name="RedirectToWWW" stopProcessing="true">
          <match url=".*" ignoreCase="true" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^mysite.com$" />
          </conditions>
          <action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>  

Open in new window

Microsoft IIS Web ServerASP.NET

Avatar of undefined
Last Comment
webressurs
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Could you add a new rule as the first rule matching the specific URL in question and use the action type of None?
Avatar of webressurs
webressurs
Flag of Norway image

ASKER

I don't know? I guess it should be possible to make an exception so that "api.mysite.com" never is redirected. I tried using "negate" in <conditions>, then "api.mysite.com" worked as it should, but mysite.com is not always correct redirected to www.mysite.com.
Avatar of webressurs
webressurs
Flag of Norway image

ASKER

Do you mean something like this?

<rule name="block" stopProcessing="true">
    <match url="^api.mysite.com$" />
    <action type="None" />
</rule>

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

That appears to be correct, based on Microsoft's documentation.
Avatar of webressurs
webressurs
Flag of Norway image

ASKER

I tried it, but the subdomain does not work.

    <rewrite>
      <rules>
        <rule name="RedirectToWWW" stopProcessing="true">
          <match url=".*" ignoreCase="true" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^mysite.com$" />
          </conditions>
          <action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
        </rule>
        <rule name="API" stopProcessing="true">
          <match url="^api.mysite.com$" ignoreCase="true" />
          <action type="None" />
        </rule>
      </rules>
    </rewrite>

Open in new window

Avatar of webressurs
webressurs
Flag of Norway image

ASKER

I also tried "negate" without Luck

    <rewrite>
      <rules>
        <rule name="RedirectToWWW" stopProcessing="true">
          <match url=".*" ignoreCase="true" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^mysite.com$" />
            <add input="{HTTP_HOST}" pattern="^api.mysite.com$" negate="true" />
          </conditions>
          <action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Retry the first attempt, but switch the order of the rules. I believe the rules engine will find the first rule that satisfies the condition, and in your first attempt, the .* would match anything and, therefore, would always be used.
Avatar of Vivek Reddy
Vivek Reddy
Flag of India image

<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions><add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
</rule>
Avatar of webressurs
webressurs
Flag of Norway image

ASKER

Shaun Kline: Unfortunatly that did not help either, api.mysite.com still stop working when I add the <rewrite> code in web.config (mysite.com).

I think this is pretty strange since api.mysite.com is one site in IIS, and mysite.com is another site in IIS. When I user url rewrite on one site (mysite.com), why does that effect another site (api.mysite.com). They also run in separate application pools.
Avatar of webressurs
webressurs
Flag of Norway image

ASKER

Is it possible to make a rule that only redirects to www.example.com if the input doesn't start with www. or api.?
ASKER CERTIFIED SOLUTION
Avatar of webressurs
webressurs
Flag of Norway image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of webressurs
webressurs
Flag of Norway image

ASKER

This solved the problem.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo