15

I have the following rule which is working well for redirecting my www requests to the root.

However I can't seem to turn it off for localhost. Here is what I have now:

    <rule name="CanonicalHostNameRule1">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://example.com/{R:1}" />
    </rule>

I've tried many things including things like:

    <rule name="CanonicalHostNameRule1">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
        <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
      </conditions>
      <action type="Redirect" url="https://example.com/{R:1}" />
    </rule>

Could you help? Regexes are my weakness alas

user1112324
  • 623
  • 1
  • 7
  • 23
  • Check [this post](https://stackoverflow.com/a/20947827/3832970). – Wiktor Stribiżew Jun 04 '18 at 07:33
  • @WiktorStribiżew I tried adding a condition but that didnt work? what am I doing wrong? – user1112324 Jun 04 '18 at 08:05
  • Maybe you need to use ``? – Wiktor Stribiżew Jun 04 '18 at 08:32
  • No it doesnt work unfortunately... – user1112324 Jun 04 '18 at 09:21
  • Try using a _URL Validator_ without _localhost_. `^(?!mailto:)(?:(?:https?|ftp):\/\/)?(?:\S+(?::\S*)?@)?(?:(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))))(?::\d{2,5})?(?:\/[^\s]*)?$` Then it won't match `localhost`. –  Jun 07 '18 at 17:46
  • 1
    Have you tried just `localhost` as your pattern – Brent Jun 07 '18 at 21:02
  • The solution can be found here: https://stackoverflow.com/questions/26219744/iis-url-rewrite-https-rule-ignoring-localhost – Paul Zahra Mar 30 '23 at 12:58
  • Does this answer your question? [IIS URL Rewrite https rule ignoring localhost](https://stackoverflow.com/questions/26219744/iis-url-rewrite-https-rule-ignoring-localhost) – Paul Zahra Mar 30 '23 at 13:00

2 Answers2

5

How about using a condition to only match requests that start with www. instead of trying to negate when you don't want the rule to apply? This avoids the need to negate localhost because localhost never matches in the conditions:

<rule name="Strip WWW" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)" />
    </conditions>
    <action type="Redirect" url="https://{C:1}/{URL}" />
</rule>

However, your example of the rule you tried (your second code block) also works for me in testing with IIS on a Windows 10 VM. I can browse to localhost without a redirect. Perhaps there is another issue here.

Brandon Miller
  • 4,695
  • 1
  • 20
  • 27
0

I would not mix the rules of different hosting environments; localhost (for local development) and www, your live environment. If you keep them separate, you don't have to enable and disable rules depending on the environment.

The rules section has a configSource attribute via which you can point to an other separate file, eg. RewriteRules.config. Doing so, web.config will look like here below.

<configuration>
    <!-- Other settings go here. -->

    <system.webServer>
        <!-- other settings go here --->

        <rewrite>
            <rules configSource="RewriteRules.config">
        </rewrite>
    </system.webServer>   
</configuration>

The RewriteRules.config file contains the rules.

<rules>
    <rule name="CanonicalHostNameRule1">
        <!-- Rule details go here -->
    </rule>
</rules>

You make a separte version of this RewriteRules.configfile per environment, containing only the appropriate rules and deploy it to the concerned webserver.

This has many benefits.

  • Only the rules for the concerned environment are being evaluated, which is better for performance.
  • It is more flexible if you have other environments like QA (http://qa. ...) and dev (http://dev. ...).
  • You don't have to worry (and test) whether the rules of one environment will interfere with the ones of an other one, here: local vs live.

The deployment of the RewriteRules.config file can be included in a deployment automation.

pfx
  • 20,323
  • 43
  • 37
  • 57