I build a website and i want redirect http (local) to https (use online). I tried using middleware, using AppServiceProvider
, .htacess
, RouteServiceProvider
but it didn't work.
1, When i use middleware
class HttpsProtocol
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if (!app()->environment('local')) {
// for Proxies
Request::setTrustedProxies([$request->getClientIp()],
Request::HEADER_X_FORWARDED_ALL);
if (!$request->isSecure()) {
return redirect()->secure($request->path(), 301);
}
}
return $next($request);
}
}
2, When i use .htancess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# 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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
public function boot()
{
if (!app()->environment('local')) {
$this->app['request']->server->set('HTTPS', true);
URL::forceScheme('https');
}
//
}
https://i.sstatic.net/4tZz0.png
It created another https link and can run. But when i use check slug automatic
https://i.sstatic.net/Kv8rh.png
and when i use Post method
https://i.sstatic.net/9vKOD.png
https://i.sstatic.net/rAoMD.png It didn't make it to https, Please help me ! (Sorry because my english)
Just open AppServiceProvider and add
\URL::forceScheme('https');
in the boot function. thats all.
public function boot()
{
\URL::forceScheme('https');
}