重定向规则不起作用 [英] Redirect rule not working
问题描述
我正在尝试为我的网站添加转发规则。如果匹配以下任何网址:
I'm trying to add a forwarding rule for my website. If any of the following URLs are matched:
- https://www.example.com
- http://example.com
- http://www.example.com
然后它应该重定向到
我正在使用URL Rewrite 2对于Windows 2016上的IIS。
I am using URL Rewrite 2 for IIS on Windows 2016.
我试图将其分解为2条规则,因为我无法想到如何做到这一点。规则的第一部分是从http到https
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>
上述作品。
现在我需要使用 https:// www 来https://这似乎没有做任何事情
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>
这意味着如果我输入 http
该规则有效,但当我输入 https://www.example.com 时,它不会转发到< a href =https://example.com =nofollow noreferrer> https://example.com
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?
{HTTP_HOST}
是其范围内的常量,因此您将从A重定向到A.
{HTTP_HOST}
is a constant in its scope, so you redirect from A to A.
查看我写的以下规则。
Check out the following rule I wrote.
规则#1 与以 www。
开头的所有主机名匹配,删除 www。
重定向到HTTPS。
Rule#1 matches with all hostnames starting with www.
, removes www.
redirects to HTTPS.
规则#2 与所有非安全请求匹配,但对localhost的请求,然后通过重复与规则#1中相同的过程重定向。
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>
这篇关于重定向规则不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!