Solved

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

Posted on 2014-11-08
12
437 Views
Last Modified: 2014-11-25
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

0
Comment
Question by:webressurs
  • 8
  • 3
12 Comments
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40431085
Could you add a new rule as the first rule matching the specific URL in question and use the action type of None?
0
 
LVL 1

Author Comment

by:webressurs
ID: 40431247
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.
0
 
LVL 1

Author Comment

by:webressurs
ID: 40431253
Do you mean something like this?

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

Open in new window

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40432814
That appears to be correct, based on Microsoft's documentation.
0
 
LVL 1

Author Comment

by:webressurs
ID: 40436865
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

0
 
LVL 1

Author Comment

by:webressurs
ID: 40436884
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

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40438347
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.
0
 
LVL 2

Expert Comment

by:Vivek Reddy
ID: 40439537
<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>
0
 
LVL 1

Author Comment

by:webressurs
ID: 40439548
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.
0
 
LVL 1

Author Comment

by:webressurs
ID: 40439615
Is it possible to make a rule that only redirects to www.example.com if the input doesn't start with www. or api.?
0
 
LVL 1

Accepted Solution

by:
webressurs earned 0 total points
ID: 40456902
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.
0
 
LVL 1

Author Closing Comment

by:webressurs
ID: 40464108
This solved the problem.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question