Reputation: 11130
I have a PHP Application deployed on Azure Website. I have configured the SSL certificate under the configuration and now I need to redirect http request to https. How can I configure this?
Upvotes: 1
Views: 1203
Reputation: 456
Well, the easiest way is to use a web.config for this purpose.
This rule will make sure all traffic is sent over 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>
This is my default configuration for PHP apps on 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>
Upvotes: 1
Reputation: 1611
This should be the same checks you would for php anywhere, not just in Azure. You should be able to check if it is on HTTPS with this:
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();
}
Upvotes: 1