简体   繁体   中英

Laravel redirect all http to https

I have a website in Laravel which i switched over to https a while ago. I have succeeded to redirect all secure non-www (https://) URL's to www ( https://www .) variants with the help of other posts, but am getting stuck on getting both non-secure variants (http://) and ( http://www .) redirected to the secure www variants ( https://www .)

When you call for the non-secure, www version: http://www.apk-vervaldatum.nl ,

It redirects to: https://www.apk-vervaldatum.nl/public/https://www.apk-vervaldatum.nl

When you call for the non-secure, non-www version: http://apk-vervaldatum.nl ,

It first redirects to: https://www.apk-vervaldatum.nl

Then redirects to: https://www.apk-vervaldatum.nl/public/https://www.apk-vervaldatum.nl

My htaccess in the root folder looks is:

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

My htacces in public folder looks is:

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

Here you have method how to enforce using HTTPS

Go to 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
    }
}

Another approach is change is .htaccess :

RewriteEngine On

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

Good luck!

your .htaccess should be :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM