14

For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.

I've added this to my HOSTS file (c:\windows\system32\drivers\etc):

127.0.0.1   www.mydomain.com

Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:

<rewrite>
    <rules>
        <rule name="mydomain.com" stopProcessing="true">
            <match url="^www.mydomain.com/(.*)" />
            <action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
        </rule>
    </rules>
</rewrite>

But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Matt Roberts
  • 26,371
  • 31
  • 103
  • 180

2 Answers2

24

I think the following should work:

<rule name="mydomain.com" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>

The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.

Martin Steel
  • 572
  • 9
  • 19
  • 1
    Almost :) I had to use a redirect (rewrite didn't work), and had to specify the full path with the host (`http://localhost/MyVDir/{R:1}`) but once I'd done that, I was in :) – Matt Roberts Jun 12 '13 at 13:52
  • Shouldn't you be escaping the periods in the pattern for your domain names? ^((mydomain\.com)|(www\.mydomain\.com))$ – Jason Butera Mar 22 '17 at 14:16
  • Good spot, luckily the any character matching from periods is unlikely to be a problem as most people won't have wwwXmydomain.com where X is a random letter. The extra parenthesis aren't needed. – Martin Steel Mar 22 '17 at 18:52
  • this also helps a lot with subdomain situations like, the main domain is gonna redirect all HTTP to HTTPS, but I don't want that to happen with my subdomain like 'app2.mydomain.com' should still respond on HTTP as well... this helps a lot by excluding the subdomain from the rule. Thank you very much! – Caio César S. Leonardi Aug 20 '20 at 19:40
2

The following is better for catching both www. and non-www. versions of the domain so that you don't have to write the domain twice, which could possibly cause errors with being twice as likely to write a typo. (The parenthesis with the ? character means optional in regex terms.)

<rule name="mydomain.com" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77