Reputation: 4691
I'm trying to add a forwarding rule for my website. If any of the following URLs are matched:
then it should redirect to
I am using URL Rewrite 2 for IIS on Windows 2016.
I'm trying to break this into 2 rules as I can't think of how else to do it. The first part of the rule is from http to https
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true"/>
<add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
The above works.
Now I need to work with https://www to https:// and this doesn't appear to be doing anything
<rule name="www to no subdomain redirect" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^www" ignoreCase="true"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}"/>
</rule>
</rules>
This means if I type in using http
the rule works, but when I type in https://www.example.com it is not forwarding to https://example.com
What am I doing wrong?
Upvotes: 0
Views: 2974
Reputation: 16950
What am I doing wrong?
{HTTP_HOST}
is a constant in its scope, so you redirect from A to A.
Check out the following rule I wrote.
Rule#1 matches with all hostnames starting with www.
, removes www.
redirects to HTTPS.
Rule#2 matches with all non-secure requests except the requests to localhost, then redirects by repeating the same process as in the Rule#1.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect-AllWWW-ToSecureNonWWW">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?:www\.)(.+)$" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}"/>
</rule>
<rule name="Redirect-AllNonSecure-ToSecureNonWWW-ExcludingLocalhost">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^localhost$" negate="true" />
<add input="{HTTPS}" pattern="^off$" />
<add input="{HTTP_HOST}" pattern="^(?:www\.)?(.+)" />
</conditions>
<action type="Redirect" url="https://{C:1}/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Upvotes: 1
Reputation: 5677
Try like this
<rules>
<clear />
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
</conditions>
<action type="Redirect" url="https://example.com/{R:1}" />
</rule>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTPS}" pattern="^OFF$" ignoreCase="true" />
<add input="{HTTP_HOST}" pattern="localhost" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
</rules>
I have two rules but only one will execute at anytime and make sure all the logic is covered.
In your 'www to no subdomain redirect' rule,you are using {HTTP_HOST} in the redirect which will be www.example.com .
Hope this helps!
Upvotes: 1