Skip to content

Instantly share code, notes, and snippets.

@reliq
Last active May 9, 2017 02:37
Show Gist options
  • Save reliq/7a5f506427eeaba6faa47ef3551f0bde to your computer and use it in GitHub Desktop.
Save reliq/7a5f506427eeaba6faa47ef3551f0bde to your computer and use it in GitHub Desktop.
Tiny Laravel 5 middleware to redirect www requests to non-www counterparts.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Routing\Middleware;
/**
* NonWWW
* Redirects any www requests to non-www counterparts.
*
* @param request The request object.
* @param $next The next closure.
* @return redirects to the secure counterpart of the requested uri.
*/
class NonWWW
{
public function handle($request, Closure $next)
{
if (substr($request->header('host'), 0, 4) == 'www.'){
$request->setTrustedProxies( [ $request->getClientIp() ] );
$request->headers->set('host', parse_url(config('app.url'), PHP_URL_HOST));
return redirect($request->getRequestUri(), 301);
}
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment