Solved

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

Posted on 2014-11-08
12
422 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
 
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now