web.config将非www重定向到www

Ana*_*and 39 iis web-config url-rewriting

我需要将非www网址重定向到www网址,用于http和https网址.我尝试在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" />
Run Code Online (Sandbox Code Playgroud)

<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" />
Run Code Online (Sandbox Code Playgroud)

它适用于非ssl网址,但在ssl的情况下,它从https://domain.com重定向 到http://www.domain.com

请帮我纠正我的规则.

Sat*_*pal 52

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

您可以创建名为MapProtocol的重写映射,您可以{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>
Run Code Online (Sandbox Code Playgroud)

参考

  • 自我注意:用你自己的域名替换`domain.com` <slaps head> (20认同)
  • 附加说明:确保将[Bindings ...]设置为包含www.domain.com和domain.com的http和https.如果您没有绑定域,则无法使用重定向规则! (7认同)
  • 值得注意的是,您需要URL Rewrite Module 2.0([http://www.iis.net/downloads/microsoft/url-rewrite ](http://www.iis.net/downloads/microsoft/url-rewrite))为此工作. (5认同)
  • 模式不应该转义点字符吗?`&lt;add input="{HTTP_HOST}" pattern="^domain\.com$" /&gt;` (3认同)

Cht*_*lek 13

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

希望这可以帮助.

  • 我已为您登录 +1 (2认同)
  • 您实际上回答了问题,而不是玩弄证书 (2认同)
  • 支持无需知道域名即可更通用和可用。 (2认同)

Mel*_*man 8

自2018年以来,考虑每次使用https(SSL)!

所以我一起使用重定向到www和https.

<rule name="Redirect to www" stopProcessing="true">
    <match url="(.*)" />
    <conditions trackAllCaptures="false">
        <add input="{HTTP_HOST}" pattern="^domain.com$" />
    </conditions>
    <action type="Redirect" url="https://www.comain.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>
Run Code Online (Sandbox Code Playgroud)

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