将Azure网站重定向到HTTPS


Redirect Azure Website to HTTPS

我在Azure网站上部署了一个PHP应用程序。我已经在配置下配置了SSL证书,现在我需要将http请求重定向到https。如何配置?

这应该与您在任何地方对php进行的检查相同,而不仅仅是在Azure中。你应该能够通过以下方式检查它是否在HTTPS上:

if ($_SERVER['HTTPS'] == 'off' || !isset($_SERVER['HTTPS']) || !$_SERVER['HTTPS']) {
    // request is not using SSL, redirect to https, or fail
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
    exit();
}

好吧,最简单的方法是使用web.config来实现此目的。

此规则将确保所有流量都通过SSL 发送

<rule name="Force HTTPS" enabled="true" stopProcessing="true">
    <match url="(.*)" ignoreCase="false" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>

这是我在Azure 上的PHP应用程序的默认配置

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <directoryBrowse enabled="false" />
        <httpErrors existingResponse="PassThrough" />
        <rewrite>
          <rules>
            <clear />
            <!-- Rewrite rules to /public by @maartenballiauw *tnx* -->
            <rule name="TransferToPublic-StaticContent" patternSyntax="Wildcard" stopProcessing="true">
              <match url="*" />
              <conditions logicalGrouping="MatchAny">
                <add input="{REQUEST_URI}" pattern="*images*" />
                <add input="{REQUEST_URI}" pattern="*css*" />
                <add input="{REQUEST_URI}" pattern="*js*" />
                <add input="{REQUEST_URI}" pattern="robots.txt" />
              </conditions>
              <action type="Rewrite" url="public/{R:0}" />
            </rule>
            <rule name="TransferToPublic" patternSyntax="Wildcard">
              <match url="*" />
              <action type="Rewrite" url="public/index.php" />
            </rule>
              <rule name="Force HTTPS" enabled="true" stopProcessing="true">
                 <match url="(.*)" ignoreCase="false" />
                 <conditions>
                     <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
                 </conditions>
                 <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
               </rule>
          </rules>
        </rewrite>
        <defaultDocument>
            <files>
                <clear />
                <add value="index.php" />
                <add value="index.html" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>