Laravel将所有http重定向到https

时间:2019-01-24 11:02:00

标签: laravel .htaccess redirect https

我在Laravel有一个网站,不久前我切换到了https。在其他帖子的帮助下,我已成功将所有安全的非www(https://)URL重定向到www(https://www。)变体,但是却陷入了同时获取两种非安全变体(http:/ /)和(http://www。)重定向到安全的www变体(https://www。)

当您要求使用非安全的www版本时: http://www.apk-vervaldatum.nl

它重定向到: https://www.apk-vervaldatum.nl/public/https://www.apk-vervaldatum.nl

当您要求使用非安全的非www版本时: http://apk-vervaldatum.nl

它首先重定向到: https://www.apk-vervaldatum.nl

然后重定向到: https://www.apk-vervaldatum.nl/public/https://www.apk-vervaldatum.nl

我在根文件夹中的htaccess看起来是:

*Options +FollowSymLinks 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ public/$1 [L]*

我的公用文件夹中的htacces外观是:

*Options -MultiViews -Indexes 
RewriteEngine On # Handle Authorization Header 
RewriteCond %{HTTP:Authorization} . 
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

# Redirect Trailing Slashes If Not A Folder... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} (.+)/$ 
RewriteRule ^ %1 [L,R=301] 

# Handle Front Controller... 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^ index.php [L]*

2 个答案:

答案 0 :(得分:0)

这里您有方法使用HTTPS强制实施

转到AppServiceProvider

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        URL::forceScheme('https'); // add this
    }
}

另一种方法是更改​​.htaccess

RewriteEngine On

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

祝你好运!

答案 1 :(得分:0)

您的.htaccess应为:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Start with www 
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]


    RewriteCond %{HTTP:X-Forwarded-Proto} !https 
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


    # 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]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

</IfModule>