【问题标题】:Laravel Redirect Http to HttpsLaravel 将 Http 重定向到 Https
【发布时间】:2023-03-24 12:07:02
【问题描述】:

我们使用 Laravel 5。为了将 http 连接重定向到 https,请使用中间件 HttpsProtocol。

namespace MyApp\Http\Middleware;

use Closure;

class HttpsProtocol {

    public function handle($request, Closure $next)
    {
            if (!$request->secure() && env('APP_ENV') === 'prod') {
                return redirect()->secure($request->getRequestUri());
            }

            return $next($request); 
    }
}

在我们的 4 个测试用例中,只有 1 个正确工作(最后一个重定向)。其他 3 案例中间件添加 url 额外 index.php。

http://www.aqualink.az/index.php ---> https://www.aqualink.az/index.php/index.php http://aqualink.az/index.php ---> https://aqualink.az/index.php/index.php https://www.aqualink.az/index.php ---> https://www.aqualink.az/index.php/index.php https://aqualink.az/index.php ---> https://aqualink.az/index.php

【问题讨论】:

标签: php laravel http https


【解决方案1】:

我用 htaccess 解决了

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

    【解决方案2】:

    我认为最好使用 Web 服务器将所有 HTTP 请求重定向到 HTTPS。 Apache 的示例 VH 配置:

    <VirtualHost test.app:80>
       ServerName test.app
       Redirect permanent / https://test.app
    </VirtualHost>
    
    <VirtualHost test.app:443>
        ....
    </VirtualHost>
    

    【讨论】:

    • 它还阐明了您对端口 80 (http) 和端口 443 (https) 有两个规则。
    【解决方案3】:

    更改虚拟主机后,您可以在公用文件夹上使用该.htaccess

    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    
    RewriteEngine On
    
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
    
    # Force SSL
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

      猜你喜欢
      • 2018-02-02
      • 2019-06-18
      • 2017-10-16
      • 1970-01-01
      • 2017-03-17
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多