Laravel 4.x http://stackoverflow.com/a/19968614/610880
Laravel 5.x http://stackoverflow.com/a/28403907/610880
May Sale! All accounts are 40% off this week.
How I can force all my routes to be HTTPS not HTTP
Because http works good,
but as I go to https all my styles do not get loaded also
action('HomeController@home');
<link rel="stylesheet" href="{{ asset('frontend/css/app.css') }}">
Thans!
Laravel 4.x http://stackoverflow.com/a/19968614/610880
Laravel 5.x http://stackoverflow.com/a/28403907/610880
I've got a helpful little middleware I use that can be applied to all routes or just specific ones:
<?php
namespace App\Http\Middleware;
use Closure;
class ForceSSL
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (!$request->secure() && in_array(env('APP_ENV'), ['stage', 'production'])) {
return redirect()->secure($request->getRequestUri());
}
return $next($request);
}
}
But, if you want to enforce SSL on all routes, you're probably best off doing that with your apache/nginx config.
I do it in the apache config.
Create a virtualhost like;
<VirtualHost *:80>
ServerName rotarota.net
ServerAlias www.rotarota.net
Redirect permanent / https://rotarota.net/
</VirtualHost>
Anything coming in on port 80 is bounced to the https version
I have made NO changes in the laravel code, and its going to be a lot quicker than booting all the framework only to redirect
I thought the problem was assets being loaded using HTTP from an HTTPS page (causing them to not be loaded at all for security reasons).
If you want to force the site to be loaded from HTTPS I prefer to do it in the web server (See How to force or redirect to SSL in nginx? ).
At the and I used
if (env('APP_ENV') === 'production') {
URL::forceSchema('https');
}
I added it on my web.php routes file at the top To force on all pages!
I am using Heroku, as I am not too experienced with it yet I went easiest route for me :)
add the code to your .htaccess file
RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
All the routes will be now forced to use https
I would place this in the AppServiceProvider in the boot() method, or maybe create a ForceHttpsMiddleware
if($this->app->environment('production')) {
URL::forceScheme('https');
}
If you want to force all your routes to be HTTPS , here is a solution that worked for me.
First create a middleware.
php artisan make:middleware ForceSSL
This is what your middleware should look like.
For those wondering about this "Issue" here's what I found...
I deployed my laravel app to Azure App Services.
In Azure app services, the app gets served entirely as an http app. Azure then uses a proxy to add https
The problem is, Laravel /sees/ the request as http.
The documentation states the url helpers base the http/https off the current request, which, in azure, always looks like http.
While it's true that you can do some modRewrite type stuff to force those http URLS to map to an HTTPS url, that doesn't really help , because the pages still render to the browser with http links.
So it's necessary to do something like Roulendz did to force laravel to render http links.
@jack edit your last line?
If you're trying to make this work with laravel 5.8+, do this:
// AppServiceProvider.php
public function boot()
{
if (env('APP_ENV') !== 'local') {
URL::forceScheme('https');
}
}
Wouldn't it make sense to build into the core?
Anyway I changed my approach a bit from @dimsav
if (env('APP_FORCE_HTTPS', false)) {
URL::forceScheme('https');
}
@emilmoe because with the correct hosting setup, this is never required.
Same solution used here - Unfortunately, I am having a Laravel app behind a load balancer redirecting HTTPS connections to HTTP connection
File modified : app/Providers/AppServiceProvider.php
Line added to the function boot() : URL::forceScheme('https');
That's a good way to solve your problem. But when deploying a Laravel application you should remove the .env file and use it only on your local development environment.
Instead, set all your production configurations inside the config directory and change your code to this:
if (config('app.env') === 'production') {
URL::forceSchema('https');
}
hey. it should be:
\URL::forceScheme('https');
*** not Schema
Just an FYI. If your hosting works correctly and sets the correct headers, you only need to add the proxy (load balancer or whatever) to the TrustProxys middleware and Laravel will automatically figure everything out. https://laravel.com/docs/8.x/requests#configuring-trusted-proxies
youre .env file is empty or removed,check youre .env file
In app/Providers/AppServiceProvider.php
Add this lines:
public function boot(): void
{
if (app()->isProduction()) {
($this->{'app'}['request'] ?? null)?->server?->set('HTTPS','on');
\Illuminate\Support\Facades\URL::forceScheme('https');
}
}
I think you should do it from the server side
Hello, i have made my changes according to all information, please confirm me is this the correct way:
use Illuminate\Support\Facades\URL;
public function boot()
{
//Lets set up HTTPS protocol for all variants
if (env('APP_ENV') === 'production' && env('ENABLE_HTTPS_SUPPORT') === 'True') {
$this->app['request']->server->set('HTTPS','on'); // Pagination Links support HTTPS
URL::forceScheme('https');
}
if (env('APP_ENV') === 'demo' && env('ENABLE_HTTPS_SUPPORT') === 'True') {
$this->app['request']->server->set('HTTPS','on'); // Pagination Links support HTTPS
URL::forceScheme('https');
}
if (env('APP_ENV') === 'local' && env('ENABLE_HTTPS_SUPPORT') === 'False') {
$this->app['request']->server->set('HTTPS','off'); // Pagination Links support HTTP
}
Paginator::useBootstrap();
}
@luckydead Set Trusted Proxies if you are behind an SSL terminating service such as Cloudflare
@Snapey thanks for the remark regarding cloudflare and so one. You are right domain is put on cloudflare dns and this will be required right?
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* @var array<int, string>|string|null
*/
protected $proxies = '*';
@luckydead not sure what you mean, but trust proxies is the way to go rather than force https scheme.
@Snapey but by enable trusted ip all range its not secure method correct me if i'm wrong, but also fetch cloudflare or other website ip's is impossible to do. As i read information by default it should be $proxies; and using only https solve the problem with cloudflare and other dns
@Snapey Thnx, you save my day
You can also modify the .htaccess file in the public folder like this:
RewriteEngine On
# Always redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Please or to participate in this conversation.