Solved

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

Posted on 2014-11-08
12
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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