IIS Rewrite rule - ignore for localhost

1.6k views Asked by At

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

2

There are 2 answers

2
Brandon Miller On BEST ANSWER

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.

0
pfx On

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.