Web.config URL重写 - HTTPS和非WWW [英] Web.config URL Rewrites - HTTPS and Non-WWW

查看:321
本文介绍了Web.config URL重写 - HTTPS和非WWW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要同时使用 https 非www 重写,同时 NOT HARDCODING域名,因为我们有很多服务器。这需要在 web.config 中,而不是在 IIS 中。

I need to have both https and non-www rewrites, while also NOT HARDCODING the domain, since we have numerous servers. This needs to be in the web.config, not in IIS.

我读过很多文章:

  • http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
  • http://madskristensen.net/post/url-rewrite-and-the-www-subdomain
  • how to set asp.net web.config rewrite http to https and www to non-www

https 重写有效,非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>

我测试了正则表达式 ^。* www。* 在一个正则表达式测试器上它匹配 www.testing.com 但不是 testing.com - 所以我会假设模式会捕获它。

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.

我需要重定向的URL:

I need the URLs to redirect from:

  • testing.com ---> https://testing.com
  • www.testing.com ---> https://testing.com
  • www.testing.com/xyz/ ---> https://testing.com/xyz/

推荐答案

是我自己的问题 - <$ c $没有DNS c> www ,因此重定向不会自行解决。

Was my own issue - there was no DNS for the www, therefore the redirect wouldn't resolve on it's own.

使用的代码:

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

这篇关于Web.config URL重写 - HTTPS和非WWW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆