Url Rewrite - Web.config - HTTPS and www

断了今生、忘了曾经 提交于 2019-12-12 03:15:33

问题


I need to be able to redirect to HTTPS (currently working) AND www (if it's not in the URL already).

If the URL does NOT have the www, then it works fine. However, when the URL DOES have www, it redirects and adds an additional www, such as https://www.www.domain.com

Note that I do not want to hard code the domain, and would like to use HTTPHOST or something equivalent.

Current rewrite rules:

    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="WWW Rewrite" enabled="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>

回答1:


Ended up with this Regex - ^((?!www).)*[a-zA-Z0-9]+$

I also had to go into IIS and change "Does not match the pattern" to "Matches the pattern"

    <rule name="Redirect to HTTPS" stopProcessing="true">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="WWW Rewrite" enabled="true" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^((?!www).)*[a-zA-Z0-9]+$" />
      </conditions>
      <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>


来源:https://stackoverflow.com/questions/38883207/url-rewrite-web-config-https-and-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!