Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS Rewrite rule - ignore for localhost

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

like image 989
user1112324 Avatar asked Jun 04 '18 07:06

user1112324


People also ask

Where are IIS rewrite rules stored?

url rewriting - IIS Rewrite rule is stored in XML file that is deleted upon Web Publish - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


1 Answers

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.

like image 161
Brandon Miller Avatar answered Sep 17 '22 15:09

Brandon Miller