在Azure中部署的Laravel Web应用程序中的HTTPS路由

时间:2016-06-24 01:07:05

标签: php laravel azure https web-config

我想强制通过HTTPS进行路由。我在Azure云平台中有一个Web应用程序,在/ public文件夹中包含以下web.config文件:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

为了强制路由,Azure says添加此内容:

<system.webServer>
   <rewrite>
      <rules>
         <rule name=”Redirect to https”>
            <match url=”(.*)”/>
            <conditions>
                <add input=”{HTTPS}” pattern=”Off”/>
                <add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
            </conditions>
            <action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}”/>
         </rule>
      </rules>
    </rewrite>
</system.webServer>

您是否知道如何更改原始web.config文件以使其适合并强制使用HTTPS?

解决

我只需要添加规则。修改后的Web配置为:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Imported Rule 1" stopProcessing="true">
          <match url="^(.*)/$" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="/{R:1}" />
        </rule>
        <rule name="Imported Rule 2" stopProcessing="true">
          <match url="^" ignoreCase="false" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
          </conditions>
          <action type="Rewrite" url="index.php" />
          <rule name=”Redirect to https”>
            <match url=”(.*)”/>
            <conditions>
                <add input=”{HTTPS}” pattern=”Off”/>
                <add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
            </conditions>
            <action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}”/>
         </rule>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

2 个答案:

答案 0 :(得分:1)

我相信你可以从Laravel方面解决这个问题。

考虑以下路线:

Route::get('signin', ['uses' => 'AuthController@signin', 'https']);

此路线仅用作https。我不得不在不久前处理这种情况。

答案 1 :(得分:1)

如果您的laravel应用程序的目录架构如下:

wwwroot/
    app/
    public/
    ...
    .env
    ...

您可以使用应用程序根目录中的内容直接创建文件web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
   <rewrite>
      <rules>
         <rule name="Redirect to https">
            <match url="(.*)"/>
            <conditions>
                <add input="{HTTPS}" pattern="Off"/>
                <add input="{REQUEST_METHOD}" pattern="^get$|^head$" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/public/{R:1}"/>
         </rule>
      </rules>
    </rewrite>
</system.webServer>
</configuration>

将所有HTTP请求重定向到HTTPS,并重定向到公用文件夹。

如有任何疑问,请随时告诉我。