Ramin Darvishov
Ramin Darvishov

Reputation: 1039

Laravel Redirect Http to Https

We use Laravel 5. For redirecting http connection to https use Middleware 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); 
    }
}

In our 4 test case correctly works only 1 (last redirect). Other 3 case Middleware adds url extra 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

Upvotes: 4

Views: 10277

Answers (3)

Ramin Darvishov
Ramin Darvishov

Reputation: 1039

I solved with 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]

Upvotes: 5

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5443

After change on virtual host,you can use that .htaccess on public folder

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

Upvotes: 2

Alexey Mezenin
Alexey Mezenin

Reputation: 163838

I think it's better to use web server to redirect all HTTP requests to HTTPS. Sample VH config for Apache:

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

<VirtualHost test.app:443>
    ....
</VirtualHost>

Upvotes: 3

Related Questions