Skip to content

Commit

Permalink
fix non-www middleware issue with trusted proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
reliq committed Mar 22, 2024
1 parent b41c1fe commit c150e32
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Http/Middleware/NonWWW.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ReliqArts\Http\Middleware;

use Closure;
use Symfony\Component\HttpFoundation\Request;

/**
* Redirects any www requests to non-www counterparts.
Expand All @@ -14,10 +15,17 @@
*/
class NonWWW
{
private const TRUSTED_PROXY_HEADERS = Request::HEADER_X_FORWARDED_FOR |
Request::HEADER_X_FORWARDED_HOST |
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_PREFIX |
Request::HEADER_X_FORWARDED_AWS_ELB;

public function handle($request, Closure $next)
{
if (str_starts_with($request->header('host'), 'www.')) {
$request->setTrustedProxies([$request->getClientIp()], config('trustedproxy.headers'));
$request->setTrustedProxies([$request->getClientIp()], self::TRUSTED_PROXY_HEADERS);
$request->headers->set('host', parse_url(config('app.url'), PHP_URL_HOST));

return redirect($request->getRequestUri(), 301);
Expand Down

0 comments on commit c150e32

Please sign in to comment.