在Web API 2 Azure网站中将https www重定向到https non-www

时间:2018-12-10 15:10:18

标签: azure iis url-rewriting azure-web-sites webapi2

在Azure门户应用中,我将流量配置为重定向到https,但是https://www不会重定向到https://

http://http://www进行的重定向均正常工作。

这些是我在azure应用程序的web.config中拥有的规则。

<system.webServer>
<rewrite>
  <rules>
   <rule name="HTTPS" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
      <match url="*" />
      <conditions>
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
    </rule>
  <rule name="NonWwwRedirect"  stopProcessing="true"> 
      <match url="(.*)" /> 
      <conditions> 
          <add input="{HTTP_HOST}" pattern="^www\.example\.com$" /> 
      </conditions> 
      <action type="Redirect" url="https://example.com/{R:1}" redirectType="Permanent" /> 
  </rule> 
  <!--To always remove trailing slash from the URL-->
    <rule name="Remove trailing slash" stopProcessing="true">
      <match url="(.*)/$" />
      <action type="Redirect" redirectType="Permanent" url="{R:1}" />
    </rule>
    <rule name="AngularJS Routes" stopProcessing="true">
      <match url=".*" />
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(signalr)" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(token)" negate="true" />
      </conditions>
      <action type="Rewrite" url="/" />
    </rule>
  </rules>
</rewrite>

如何实现所需的重定向?

3 个答案:

答案 0 :(得分:0)

怎么样:

<rewrite>
  <rules>
    <rule name="Redirect www OR non-https to https://" enabled="true" stopProcessing="true">
      <match url=".*" ignoreCase="true" />
      <conditions logicalGrouping="MatchAny>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
        <add input="{HTTPS}" pattern="off" />
      </conditions>
      <action type="Redirect" url="https://{C:1}/{R:0}" appendQueryString="true" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>

答案 1 :(得分:0)

问题出在DNS记录上。主机为www的记录指向不同的IP。将其更改为与@ host相同的IP,即可解决问题

答案 2 :(得分:-1)

您可以参考以下代码:

<rule name="NonWwwRedirect" stopProcessing="true">
    <match url=".*"/>
    <conditions>
        <add input="{HTTP_HOST}" pattern="^www\.(.*)$" />
    </conditions>
    <action type="Redirect" url="https://example.com/{R:0}" redirectType="Permanent" />
</rule> 

注意:请确保已分配给站点的example.com主机名,以便您可以成功访问它。