Перенаправить веб-сайт Azure на HTTPS

У меня есть приложение PHP, развернутое на веб-сайте Azure. Я настроил SSL-сертификат в соответствии с настройкой, и теперь мне нужно перенаправить HTTP-запрос на https. Как я могу настроить это?

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

Другие решения

Ну, самый простой способ — это использовать 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>

Это моя конфигурация по умолчанию для приложений PHP в Azure

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