简体   繁体   中英

2 domains, 1 site, url rewrite from non-www to www with https

I have an application I wrote in ASP.NET C#. Two domains point to the site. In the web.config I'd like a url rewrite so if http://domain.com is entered it redirects to https://www.domain.com

In addition if http://domain2.com is entered it should redirect to https://www.domain2.com

I see a bunch of examples when its just one domain. Wondering if its possible when 2 domains point to the same site. When I tried I got redirect loops.

You can try something like this:

<rule name="NON-WWW HTTPS DOMAIN" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^domain\.com$" />
    <add input="{HTTPS}" pattern="^ON$" />
  </conditions>
  <action type="Redirect" url="https://www.domain.com/{R:0}" />
</rule>
<rule name="Redirect to domain HTTPS" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^[www\.]*domain\.com$" />
    <add input="{HTTPS}" pattern="^OFF$" />
  </conditions>
  <action type="Redirect" url="https://www.domain.com/{R:0}" />
</rule>
<rule name="Redirect to domain2 HTTPS" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^[www\.]*domain2\.com$" />
    <add input="{HTTPS}" pattern="^OFF$" />
  </conditions>
  <action type="Redirect" url="https://www.domain2.com/{R:0}" />
</rule>
<rule name="NON-WWW HTTPS DOMAIN 2" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTP_HOST}" pattern="^domain2\.com$" />
    <add input="{HTTPS}" pattern="^ON$" />
  </conditions>
  <action type="Redirect" url="https://www.domain2.com/{R:0}" />
</rule>

In order to redirect https://domain.com or https://domain2.com to https://www.domain.com and https://www.domain2.com you need to enable https for: domain.com/www.domain.com and domain2.com/www.domain2.com. It must be enabled in IIS because otherwise IIS will block request for not binded https request and the request will not even be processed by rewrite url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM