【问题标题】:iis url redirect http to non www httpsiis url 将 http 重定向到非 www https
【发布时间】:2013-06-11 05:11:44
【问题描述】:

我需要重定向

www.domain.dehttps://domain.de -作品

http://www.domain.dehttps://domain.de -作品

http://domain.dehttps://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>

【问题讨论】:

  • 我假设 rewrite 正确地位于其开始标签内,即使在此处的代码中缺少开始 V 字形。您能详细说明“导入规则 1”吗?

标签: redirect iis web


【解决方案1】:

我认为这对你有用,搜索模式有可选的 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>

【讨论】:

  • 角色可以放在哪里?在根目录的 web.config 文件中?
  • @David Martin,{https}://www.domain.com 到 {https}://domain.com 怎么样
  • @RohitArora 我不确定我是否完全理解您的问题,我可以建议您发布一个新问题吗?
  • @DavidMartin 简而言之,它不适合我。我也希望 IIS 8 也一样。感谢您的回复!
  • 如果您希望重定向保持在同一页面(保留路径信息)而不是转到网站根目录,请将操作更改为:&lt;action type="Redirect" url="https://{C:2}/{R:1}" redirectType="Permanent" /&gt;
【解决方案2】:

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

  1. 为 www.yourdomain.com 添加 DNS 条目以引用您服务器的公共 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

【讨论】:

    【解决方案3】:

    如果您想要比您的三个示例更灵活的东西,请将您的 HTTP_HOST 模式更改为:\w+\.\w+$。这适用于所有三个示例以及其他任何示例,例如 subdomain.abcdef.domain.de

    如果您使用此正则表达式,请将其括在括号中或在您的操作中将 C:1 更改为 C:0。

    【讨论】:

      【解决方案4】:

      这对我有用:

          <rule name="NameRule1" stopProcessing="true" enabled="true" >
              <match url="(.*)" />
              <conditions>
                <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
              </conditions>
              <action type="Redirect" url="http://example.com/{R:1}" />
          </rule>
      

      我来自:https://medium.com/iis-and-windows-server/redirect-url-from-www-to-non-www-in-iis7-5-4d2909b9704

      【讨论】:

        【解决方案5】:

        https://www.example.com 重定向到 https://example.com 我必须执行 2 个步骤

        1. 选择网站,点击“URL Rewrite” -> 点击“Add Rule” -> 选择“Cononcial domain name” -> 选择您的域名(不带 www) -> 确保规则位于顶部 -> 突出显示规则并单击“编辑”-> 并在规则末尾更新“重定向 URL”以包含“s”-https://example.com/{R:1}

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

        在完成这两个步骤之后,它才能按预期工作

        【讨论】:

          【解决方案6】:

          这些重写规则与以下 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>
          

          【讨论】:

            猜你喜欢
            • 2018-07-25
            • 2019-12-13
            • 1970-01-01
            • 1970-01-01
            • 2018-09-25
            • 1970-01-01
            • 1970-01-01
            • 2018-09-26
            • 2017-11-24
            相关资源
            最近更新 更多