mardi 24 mai 2016

Redirect domain name in Laravel

I'm wanting to redirect a short link domain in Laravel 5, e.g. xyz.com to examplesite.com whilst maintaining the URI request. For example:

http://ift.tt/1OTHTse

will redirect to:

http://ift.tt/1TAXH1Y

I've tried to use a domain group in the routes for this, but it did not seem to work on domain name level, only sub-domain. The other option I opted for is the MiddleWare route.

I have set up a working middleware "RedirectsMiddleware" and have the following in my routes file:

Route::group(['middleware' => ['redirects'] ], function () {
    Route::get('/', 'HoldingSiteController@home');
});

My RedirectsMiddleware looks like this:

...
    public function handle($request, Closure $next)
    {
        $protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';        
        $host = $_SERVER['SERVER_NAME'];
        $defaulthost = 'example.com';

        if($host != $defaulthost) {

            return Redirect::to($protocol . $defaulthost . $_SERVER['REQUEST_URI']);
        }

        return $next($request);
    }
...

When requesting just "example.com" or "http://ift.tt/1OTIIRM" it redirects fine. Any route added to the end throws an exception:

NotFoundHttpException in RouteCollection.php line 161:



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1XQtKxH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire