简体   繁体   English

Web.config URL重写 - HTTPS和非WWW

[英]Web.config URL Rewrites - HTTPS and Non-WWW

I need to have both https and non-www rewrites, while also NOT HARDCODING the domain , since we have numerous servers. 我需要同时使用httpsnon-www重写,同时也不需要HARDCODING域 ,因为我们有很多服务器。 This needs to be in the web.config , not in IIS . 这需要在web.config ,而不是在IIS

I've read numerous articles: 我读过很多文章:

The https rewrite works, the non-www does not. https重写有效, non-www无效。

    <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="Remove WWW" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <!--<add input="{CACHE_URL}" pattern="*://www.*" />-->
        <!--<add input="{HTTP_HOST}" pattern="*://www.*" />-->
        <add input="{HTTP_HOST}" pattern="^.*www.*" />
      </conditions>
      <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
      // i've also tried
      // url="{C:2}/{R:1}"
      // url="{C:1}/{C:2}"
    </rule>

I tested the regex for ^.*www.* on a regex tester and it was matching www.testing.com but not testing.com - so I would assume the pattern would catch it. 我在正则表达式测试器上测试了正则表达式为^.*www.*它匹配www.testing.com而不是testing.com - 所以我认为该模式会捕获它。

I need the URLs to redirect from: 我需要重定向的网址:

Was my own issue - there was no DNS for the www , therefore the redirect wouldn't resolve on it's own. 是我自己的问题 - www没有DNS,因此重定向不会自行解决。

Code used: 使用的代码:

    <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="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>

Add a "Canonical domain name" rule. 添加“规范域名”规则。 IIS 10

The soulution Rob found works fine as well. Rob发现的苏格兰也很好。 Mine seems to be a bit easier IMHO. 我似乎有点容易恕我直言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Web.config将http + www和http non www和https non-www重写为https:// www - Web.config rewrite http + www and http non www and https non-www to to https://www web.config使用规则将非www或非https重定向到https:// www - web.config redirect non-www OR non-https to https://www using rules 如何设置asp.net web.config将http重写为https,将www重写为非www - how to set asp.net web.config rewrite http to https and www to non-www web.config中的“非www到www”和“ https”重写规则,而不是localhost ASP.NET MVC - “non-www to www” and “https” rewrite rule in web.config but not localhost ASP.NET MVC 如何通过 web.config 将 http 重定向到 https 并将 www 重定向到非 www? - How to redirect http to https and www to non-www via web.config? 从www重定向到非www web.config asp.net - Redirect from www to non-www web.config asp.net Web.config URL重写-强制www前缀和https - Web.config URL rewrite - force www prefix and https web.config中具有尾部斜杠301重定向的非www(单重定向) - non-www with trailing slash 301 redirect in web.config (Single Redirect) 2个域,1个站点,URL从非www改写为带有https的www - 2 domains, 1 site, url rewrite from non-www to www with https Web.Config-URL重写为WWW,HTTPS和主域-URL链问题? - Web.Config - URL rewrite to WWW, HTTPS and Primary Domain - URL Chain Problems?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM