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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With