Перенаправление https с www на https без www на веб-сайте web api 2 azure

В приложении портала Azure я настроил перенаправление трафика на https, но https://www не будет перенаправлять на https://

Перенаправление с http://, http://www работает корректно.

Это правила, которые у меня есть в web.config в приложении Azure.

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

How can I achieve the needed redirect?


person Dazhush    schedule 10.12.2018    source источник


Ответы (3)


Проблема была с записями DNS. Запись с хостом www была указана на другой IP. Изменил его на тот же IP, что и @host, что решило проблему

person Dazhush    schedule 17.12.2018

Как насчет:

<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>
person Leo    schedule 11.12.2018

Вы можете обратиться к коду, как показано ниже:

<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 назначено сайту, чтобы вы могли успешно связаться с ним.

person Joey Cai    schedule 12.12.2018