Last active
May 9, 2017 02:37
-
-
Save reliq/7a5f506427eeaba6faa47ef3551f0bde to your computer and use it in GitHub Desktop.
Tiny Laravel 5 middleware to redirect www requests to non-www counterparts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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