This is for Larave 5.2.x and higher. If you want to be able to serve some content over HTTPS and another over HTTP, here is a solution that worked for me. You may wonder why someone wants to serve only some content via HTTPS? Why not serve everything over HTTPS?
Although serving the entire site via HTTPS is quite normal, interrupting everything over HTTPS has additional costs on your server. Remember that encryption is not cheap. Slight overhead also affects the response time of your application. You can argue that the hardware is cheap and the impact is negligible, but I digress :) I do not like the idea of ββshowing on large pages with images, etc. Via https. So there you go. This is similar to what others offer above using middleware, but it is a complete solution that allows you to switch between HTTP / HTTPS.
Create middleware first.
php artisan make:middleware ForceSSL
Here's what your middleware should look like.
<?php namespace App\Http\Middleware; use Closure; class ForceSSL { public function handle($request, Closure $next) { if (!$request->secure()) { return redirect()->secure($request->getRequestUri()); } return $next($request); } }
Please note that I do not filter based on the environment, because I have HTTPS settings for both the local developer and production, so there is no need.
Add the following to your routeMiddleware \ App \ Http \ Kernel.php so that you can choose which route group should use SSL.
protected $routeMiddleware = [ 'auth' => \App\Http\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'can' => \Illuminate\Foundation\Http\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'forceSSL' => \App\Http\Middleware\ForceSSL::class, ];
Next, I would like to provide two main groups of login / registration, etc. And everything else is behind Auth middleware.
Route::group(array('middleware' => 'forceSSL'), function() { /*user auth*/ Route::get('login', 'AuthController@showLogin'); Route::post('login', 'AuthController@doLogin'); // Password reset routes... Route::get('password/reset/{token}', 'Auth\PasswordController@getReset'); Route::post('password/reset', 'Auth\PasswordController@postReset'); //other routes like signup etc }); Route::group(['middleware' => ['auth','forceSSL']], function() { Route::get('dashboard', function(){ return view('app.dashboard'); }); Route::get('logout', 'AuthController@doLogout'); //other routes for your application });
Make sure your middleware is correctly applied to your routes from the console.
php artisan route:list
You have now protected all forms or confidential areas of your application. The key now is to use the view template to define secure and public (non-https) links.
Based on the above example, you would make your protected links as follows:
<a href="{{secure_url('/login')}}">Login</a> <a href="{{secure_url('/signup')}}">SignUp</a>
Unprotected links can be represented as
<a href="{{url('/aboutus',[],false)}}">About US</a></li> <a href="{{url('/promotion',[],false)}}">Get the deal now!</a></li>
This makes the full url such as https: // yourhost / login and http: // yourhost / aboutus
If you have not provided a fully qualified URL with http and use the relative URL of the link ('/ aboutus'), then https will be saved after the user visits the protected site.
Hope this helps!