Redirect rule not working

做~自己de王妃 提交于 2019-12-13 07:44:28

问题


I'm trying to add a forwarding rule for my website. If any of the following URLs are matched:

  1. https://www.example.com
  2. http://example.com
  3. http://www.example.com

then it should redirect to

to https://example.com

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?


回答1:


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>



回答2:


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!



来源:https://stackoverflow.com/questions/44054732/redirect-rule-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!