Search code examples
amazon-web-servicesamazon-elastic-beanstalklaravel-8laravel-routing

Custom route in Laravel 8 to listen to HTTP request only


My application works with stability on HTTPS protocol with port 443 but I'm using AWS EBS where health checks are always performed on HTTP with port 80. I've created middleware for enforcing HTTPS on all routes but when I enabled it, the health check fails on AWS but the website works properly.

HTTPsForce middleware handle function:

public function handle($request, Closure $next)
{

    if (!$request->secure() && app()->environment('production')) {
        return redirect()->secure($request->getRequestUri());
    }
    
    return $next($request);
}

How can I create a custom route which listens to port 80 with HTTP protocol always so the health check by AWS would perform on HTTP route while my whole website working in HTTPS?


Solution

  • You can exclude middleware on specific routes:

    Route::get('/', [Controller::class, 'action'])->withoutMiddleware([Middleware::class]);
    

    https://laravel.com/docs/9.x/middleware#excluding-middleware