将Azure网站重定向到HTTPS

时间:2014-09-16 11:56:51

标签: php azure

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

2 个答案:

答案 0 :(得分:1)

这应该与你在任何地方对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();
}

答案 1 :(得分:1)

嗯,最简单的方法是使用 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>