Migrating Laravel 5 HTTPS Redirect Logic from Filters to Middleware
In Laravel 4 and early 5, forcing traffic onto SSL was often handled via a before filter in filters.php. The pattern looked like this:
App::before(function($request) {
if (! Request::secure()) {
return Redirect::secure(Request::path());
}
});
That code works, but it couples global request handling to a legacy lifecycle hook. When upgrading to Laravel 5, the filters.php file is gone by default, and the framework encourages moving such logic into middleware. However, there's nuance when you have multiple domainsâsome with SSL and some withoutâand only want to redirect non-secure requests on specific hosts.
Why Middleware Over Filters?
Filters are synchronous hooks that run before every request regardless of route or controller. They're simple for basic tasks like CSRF protection, but they're less flexible than middleware. Middleware allows you to:
- Apply logic only to specific routes or groups.
- Return early from the request lifecycle without executing controllers or actions.
- Compose multiple concerns cleanly within
app/Http/Middleware.
For HTTPS enforcement, middleware is cleaner because it lives in the standard directory structure and integrates naturally with Laravel's HTTP kernel.
Handling Mixed Domains Gracefully
The original question highlights a common scenario: three domains point to the same app, but only two use SSL. The third is a fallback domain without certificates. A blanket redirect on all non-secure requests could break that fallback site or create confusing loops if the fallback doesn't support HTTPS at all.
You need conditional logic to decide when to enforce HTTPS. Laravel 5 gives you access to the host via $request->host() inside middleware, letting you implement domain-specific rules.
Implementing Conditional HTTPS Enforcement
Create a new middleware file, for example app/Http/Middleware/EnforceHttpsOnSecureDomains.php:
<?php
namespace App\Http\Middleware;
use Closure;
class EnforceHttpsOnSecureDomains
{
public function handle($request, Closure $next)
{
// Define which hosts require HTTPS
$secureHosts = ['example.com', 'www.example.com'];
if ($this->isNonSecureRequest($request) && in_array($request->host(), $secureHosts)) {
return redirect()->secure($request->fullUrl() ?: $request->path());
}
return $next($request);
}
protected function isNonSecureRequest($request)
{
return ! $request->isSecure();
}
}
Register this middleware in app/Http/Kernel.php under the $routeMiddleware array:
protected $routeMiddleware = [
// ...
'enforce-https' => \App\Http\Middleware\EnforceHttpsOnSecureDomains::class,
];
Then apply it selectively to routes that need HTTPS enforcement:
Route::middleware('enforce-https')->group(function () {
Route::get('/', function () {
// Your root route logic
});
});
This ensures the fallback domain isn't affected unless you explicitly add it to $secureHosts.
Trade-offs and Pitfalls
- Redirect Loops: Always use
->secure()or construct the redirect URL with a scheme prefix (https://) to avoid infinite loops. Laravel's helper methods handle this safely. - Performance: Redirecting adds an extra round-trip. For high-traffic sites, consider offloading HTTPS enforcement to infrastructure (load balancers, CDN) instead of application logic.
- Cloudflare Integration: As noted in the source material, if you're using Cloudflare, you can configure Page Rules at the edge to force SSL without touching app code. This reduces server load and centralizes security policy management.
When to Use Infrastructure Over Application Logic
If all your domains are under your control and support SSL certificates, enforcing HTTPS at the load balancer or CDN level is often preferable. It avoids coupling business logic with networking concerns and scales better under load. Application-level middleware remains valuable when you need fine-grained controlâsuch as handling mixed-domain setups like in this case.
By moving from filters to middleware and adding conditional logic based on hostnames, Laravel 5 applications can handle complex SSL requirements cleanly without sacrificing flexibility or introducing unnecessary complexity.