【问题标题】:web.config redirect non-www to wwwweb.config 将非 www 重定向到 www
【发布时间】:2022-03-04 19:23:53
【问题描述】:

我需要将非 www url 重定向到 http 和 https url 的 www url。我在 web.config 中尝试了以下规则。

<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
    <add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" redirectType="Permanent" />

<rule name="Redirect to WWW https" stopProcessing="true">
<match url=".*" />
<conditions>
    <add input="{HTTPS}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent" />

它非常适用于非 ssl url,但如果是 ssl,它会从 https://domain.com 重定向到 http://www.domain.com

请帮我改正我的规则。

【问题讨论】:

  • 第一个规则同时捕获请求的类型,第二个将不被处理

标签: iis url-rewriting web-config


【解决方案1】:

要获得适用于Match AnyMatch All 情况的更安全规则,您可以使用重写映射解决方案。这是一个非常好的解决方案,唯一的缺点是设置它的额外工作量非常小,因为您需要在创建规则之前创建一个重写映射。换句话说,如果您选择将此作为处理协议的唯一方法,那么您将是安全的。

您可以创建一个名为 MapProtocol 的 Rewrite Map,您可以在任何规则操作中使用{MapProtocol:{HTTPS}} 作为协议。

<rewrite>
  <rules>
    <rule name="Redirect to www" stopProcessing="true">
      <match url="(.*)" />
      <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^domain.com$" />
      </conditions>
      <action type="Redirect" 
        url="{MapProtocol:{HTTPS}}://www.domain.com/{R:1}" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="MapProtocol">
      <add key="on" value="https" />
      <add key="off" value="http" />
    </rewriteMap>
  </rewriteMaps>
</rewrite>

Reference

【讨论】:

  • 值得注意的是,您需要 URL Rewrite Module 2.0 (http://www.iis.net/downloads/microsoft/url-rewrite) 才能工作。
  • 自我说明:用你自己的域名替换domain.com
  • 附加说明:确保将 [Bindings...] 设置为包括 www.domain.com 和 domain.com 的 http 和 https。如果不绑定域,则不能使用重定向规则!
  • 如何将example.com/subsite从非www重定向到www?我试过上面的一个,但它只适用于主页而不是内页。
  • 模式不应该转义点字符吗? &lt;add input="{HTTP_HOST}" pattern="^domain\.com$" /&gt;
【解决方案2】:

这里的其他答案是不必要的,这是我使用的规则(只需复制和粘贴):

<rule name="ensurewww" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
  </conditions>
  <action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>

这将重定向到 www 版本,同时保留 HTTP 和 HTTPS 协议

希望这会有所帮助。

【讨论】:

  • 我已登录为你 +1
  • abc.com ->www.abc.com 真 abc.com/a.aspx->www.com/a.aspx 假。请帮我。试过:
  • 您实际上是在回答问题,而不是在玩弄证书
  • 赞成更通用和更实用,而无需知道域名。
【解决方案3】:

自 2018 年起,考虑每次都使用 https (SSL)!

所以我同时使用重定向到 www 和 https。

<rule name="Redirect to www" stopProcessing="true">
    <match url="(.*)" />
    <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^example.com$" />
    </conditions>
    <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="Permanent" />
</rule>

<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAny">
        <add input="{SERVER_PORT_SECURE}" pattern="^0$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>

更多信息在这里: https://forums.realmacsoftware.com/t/effective-july-2018-google-s-chrome-browser-will-mark-non-https-sites-as-not-secure/18597

【讨论】:

    【解决方案4】:

    我知道这是一篇旧帖子,但尚未完全回答。我最终扩展了Satpals answer

    第一条规则捕获 http + www,第二条规则捕获非 www

    由于某种原因,我无法在第一条规则中使用 MapProtocol,但它适用于直接写入 url 的 https。

    <rewrite>
      <rules>
        <rule name="Special case www &amp; HTTPS off" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^www\.example\.com$" />
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://www.example.com/{R:1}" redirectType="SeeOther" />
        </rule>
        <rule name="Redirect to www &amp; HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions trackAllCaptures="false">
            <add input="{HTTP_HOST}" pattern="^example\.com$" />
          </conditions>
          <action type="Redirect" url="{MapProtocol:{HTTPS}}://www.example.com/{R:1}" redirectType="SeeOther" />
        </rule>
      </rules>
      <rewriteMaps>
        <rewriteMap name="MapProtocol">
          <add key="on" value="https" />
          <add key="off" value="http" />
        </rewriteMap>
      </rewriteMaps>
    </rewrite>
    

    【讨论】:

      【解决方案5】:

      这对我来说很好用:-

      <rewrite>
        <rules>
         <rule name="Redirect to WWW" stopProcessing="true">
      	<match url=".*" />
      	<conditions>
      	<add input="{HTTP_HOST}" pattern="^myExample.in$" />
      	</conditions>
      	<action type="Redirect" url="https://www.myExample.in/{R:0}" redirectType="Permanent" />
      </rule>
       </rules>
      </rewrite>

      【讨论】:

        【解决方案6】:

        这篇文章基本上是为那些需要在windows主机中将非www重定向到www URL的人准备的。

        在 web.config 文件中如下代码:

        <system.webServer>
        <rewrite>
        <rules>
        <rule name=”Redirect example.com to www.example.com HTTP” patternSyntax=”ECMAScript” stopProcessing=”true”>
        <match url=”.*” />
        <conditions>
        <add input=”{HTTP_HOST}” pattern=”^example.com$” />
        <add input=”{HTTPS}” pattern=”off” />
        </conditions>
        <action type=”Redirect” url=”http://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
        </rule>
        <rule name=”Redirect domain.com to www.example.com HTTPS” patternSyntax=”ECMAScript” stopProcessing=”true”>
        <match url=”.*” />
        <conditions>
        <add input=”{HTTP_HOST}” pattern=”^domain.com$” />
        <add input=”{HTTPS}” pattern=”on” />
        </conditions>
        <action type=”Redirect” url=”https://www.example.com/{R:0}” redirectType=”Permanent” appendQueryString=”true”/>
        </rule>
        </rules>
        </rewrite>
        </system.webServer>
        

        请注意,在上面的代码中,有两条规则用于http,另一条用于https。为了避免同一规则中http和https的冲突,这里使用了两种不同的模式。

        我希望它对你们有用……!!

        【讨论】:

          【解决方案7】:

          按照下面的步骤进行

          1) 登录您网站的 Cpanel

          2)点击主机设置

          click here to See how to do

          3) 选择首选域为 www.yourdomainame.com 例如:www.emodafinil.com

          click here to See how to do

          【讨论】:

          • 这根本不是问题所在。问题是关于 IIS,而不是您拥有的任何特定托管服务提供商。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-03-30
          • 2018-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-31
          相关资源
          最近更新 更多