IIS URL重定向HTTP到非www HTTPS

17

我需要将

www.domain.de 重定向到 https://domain.de -有效

http://www.domain.de 重定向到 https://domain.de -有效

http://domain.de 重定向到 https://domain.de -无效

rewrite>
  <rules>
    <rule name="Imported Rule 1" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="false" />
      <conditions logicalGrouping="MatchAll">
        <add input="{HTTP_HOST}" pattern="^www\.(.+)$" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

我假设重写已经正确地位于其开始标签内,尽管在这里的代码中缺少开始尖括号。你能详细说明一下“导入规则1”吗? - Roshan
8个回答

34

我认为这适合你,搜索模式有可选的www并使用回溯引用C:2进行重定向,该规则具有条件,只针对非https运行。

这是模式:

"^(www\.)?(.*)$"

其中:

{C:0} - www.domain.de
{C:1} - www.
{C:2} - domain.de

以下是完整的规则:

  <rewrite>
    <rules>
      <rule name="SecureRedirect" stopProcessing="true">
        <match url="^(.*)$" />
        <conditions>
          <add input="{HTTPS}" pattern="off" />
          <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
        </conditions>
        <action type="Redirect" url="https://{C:2}" redirectType="Permanent" />
      </rule>
    </rules>
  </rewrite>

2
角色应该放在哪里?是在根目录下的web.config文件中吗? - SaidbakR
@RohitArora 我不确定我完全理解你的问题,我可以建议你发布一个新问题吗? - David Martin
@DavidMartin 简而言之,这对我不起作用。我也想要适用于IIS 8的相同方法。感谢您的回复! - Rohit Arora
3
如果希望重定向停留在当前页面(保留路径信息),而不是跳转到网站根目录,请将操作更改为以下内容:<action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" /> - Mart

7

被接受的答案没有处理特殊情况https://www.domain.de

以下规则可以完成整个工作:

    <rewrite>
      <rules>
        <rule name="Redirect to HTTPS without www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^OFF$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)?(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
        <rule name="Special case for HTTPS with www" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{HTTPS}" pattern="^ON$" />
            <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
          </conditions>
          <action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>

4

如果你想将www重定向到非www:

  1. 添加DNS条目,将www.yourdomain.com指向您服务器的公共IP
  2. 然后需要编辑网站的绑定,并“添加绑定” www.yourdomain.com
  3. 使用iis为您的网站添加一个重写规则:
<rule name="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
  <match url="*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="*://www.*" />
  </conditions>
  <action type="Redirect" url="{C:1}://{C:2}" redirectType="Permanent" />
</rule>

参考文献: http://madskristensen.net/post/url-rewrite-and-the-www-subdomain

本文介绍如何通过使用 URL 重写规则,将非 www 前缀转换为带有 www 的前缀。这是一种较好的做法,可以统一您网站中所有页面的标准。

2
如果您想要比您提供的三个示例更灵活的内容,请将HTTP_HOST模式更改为:\w+\.\w+$。这将适用于所有三个示例以及其他任何内容,例如subdomain.abcdef.domain.de
如果您使用此正则表达式,请将其括在括号中或将操作中的C:1更改为C:0。

0

这是不正确的,因为问题涉及到HTTPS。 - Lex Li

0

尝试了许多解决方案,但以下方法对我有效:

  1. 右键单击域名并添加站点绑定,在www下添加一个更多的域名:

enter image description here

2. 重新启动服务。应该可以解决问题。


  1. 更改绑定不需要服务重启。
  2. 你还没有尝试足够多。
- Lex Li

-1

https://www.example.com重定向到https://example.com,我需要执行2个步骤

  1. 选择网站,点击“URL 重写” -> 点击“添加规则” -> 选择“规范域名” -> 选择您的域名(不包括 www) -> 确保规则在顶部 -> 高亮显示规则并单击“编辑” -> 并更新规则末尾的“重定向 URL”以包括“s” - https://example.com/{R:1}

  2. 我还必须创建一个新网站,使用域名www.example.com,新应用程序池,并选择 SSL 证书,然后将其指向一个包含以下内容的文件夹(由于某种原因,StackOverflow 不会显示我粘贴下面的重写规则代码,但只需搜索 IIS 重写规则即可)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Redirect to https" stopProcessing="true">
       <match url=".*" />
       <conditions>
         <add input="{HTTPS}" pattern="off" ignoreCase="true" />
       </conditions>
       <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
     </rule>
     <rule name="NonWwwRedirect"  stopProcessing="true"> 
        <match url="(.*)" /> 
        <conditions> 
            <add input="{HTTP_HOST}" pattern="^www.example\.com$" /> 
        </conditions> 
        <action type="Redirect" url="https://example.com/{R:1}" /> 
    </rule> 
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

只有完成这两个步骤,它才能按预期工作。


你应该阅读Markdown指南,以学习格式化代码块的语法。此外,通常情况下你不需要新建一个网站,而是向现有网站添加新的绑定。 - Lex Li

-1

这些重写规则匹配以下URL:

它们都将重定向到:https://example.com

由于不同的规则,这些重写规则可能会导致重定向两次。(我是一个正则表达式的新手)

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
                    <match url="*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
                </rule>
                <rule name="WWW" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
                    </conditions>
                    <action type="Redirect" url="https://example.com{PATH_INFO}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接