Solved

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

Posted on 2014-11-08
12
401 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 25

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 25

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 25

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Debug Tools to analyse IIS process: This article focus on taking memory dumps from IIS to determine which code is taking more time and to analyse which calls hangs/causes more CPU usage. To take dumps,download the following. Install1: To st…
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.…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

16 Experts available now in Live!

Get 1:1 Help Now