【问题标题】:Web.config Ignore all subdomains while redirecting non www to wwwWeb.config 在将非 www 重定向到 www 时忽略所有子域
【发布时间】:2019-01-23 07:40:45
【问题描述】:

我正在使用 web.config 将所有非 www 重定向到运行正常的 www。如何添加规则以忽略所有子域,以便 http://example.com 重定向到 http://www.example.com 但任何子域 http://*.example.com 或 http://one.example.com 不重定向到 www。 (我无法添加子域列表)。

<rule name="Redirect to www">
  <match url=".*" />
	  <conditions logicalGrouping="MatchAny">
		  <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
		</conditions>
	<action type="Redirect" url="http://www.{HTTP_HOST}" redirectType="Permanent"/>
</rule>

【问题讨论】:

    标签: azure redirect azure-web-app-service


    【解决方案1】:

    我认为 Joey 的回答只涵盖了域名以 .com 结尾的情况,无法处理像 .co.uk .co.za .com.au这样的情况> 等。您可以进一步扩展正则表达式,但允许使用不同类型的域 [a-z] 最终也会重定向子域,根据问题。

    因此,为了实现这一目标,您必须处理您认为在您的情况下可能出现的特定情况。

    <rule name="Redirect to www">
      <match url=".*" />
      <conditions logicalGrouping="MatchAny">
        <add input="{HTTP_HOST}" pattern="^([a-z-]+[.](com|co|ca|net)([.](uk|sg|au)){0,1})$" negate="false" />
      </conditions>
      <action type="Redirect" url="http://www.{HTTP_HOST}" redirectType="Permanent"/>
    </rule>

    您可以根据自己的喜好修改正则表达式。

    【讨论】:

      【解决方案2】:

      您可以尝试使用以下代码:

      <rules>
          <rule name="Redirect to www">
              <match url="(.*)" />
              <conditions>
                  <add input="{HTTP_HOST}" pattern="^example\.com$" negate="true" />
                  <add input="{HTTP_HOST}" pattern="^one\.example\.com$" negate="true" />
              </conditions>
              <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"/>
          </rule>
      </rules>
      

      【讨论】:

      • 这需要添加子域,例如一,我可以对所有进入的子域使用某种 * 吗?
      • 你可以试试&lt;add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$" negate="true" /&gt;。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 2022-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-02-24
      • 1970-01-01
      相关资源
      最近更新 更多