-
-
Save enijar/2e33038e5851b70168e0 to your computer and use it in GitHub Desktop.
Laravel 5.1 Domain Redirect Middleware
This file contains hidden or 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 stdClass; | |
class DomainRedirect | |
{ | |
private $redirects = [ | |
'za.grayling.com' => 'africa', | |
'africa.grayling.com' => 'africa', | |
'grayling.ng' => 'africa', | |
'grayling.at' => 'at', | |
'at.grayling.com' => 'at', | |
'grayling.be' => 'be', | |
'grayling.eu' => 'be', | |
'bg.grayling.com' => 'bg', | |
'cn.grayling.com' => 'cn', | |
'grayling.hr' => 'hr', | |
'grayling.cz' => 'cz', | |
'cz.grayling.com' => 'cz', | |
'grayling.fr' => 'fr', | |
'fr.grayling.com' => 'fr', | |
'grayling.de' => 'de', | |
'de.grayling.com' => 'de', | |
'hk.grayling.com' => 'hk', | |
'grayling.hu' => 'hu', | |
'hu.grayling.com' => 'hu', | |
'me.grayling.com' => 'middle_east', | |
'meta.grayling.com' => 'middle_east', | |
'grayling.nl' => 'nl', | |
'nl.grayling.com' => 'nl', | |
'grayling.pl' => 'pl', | |
'pl.grayling.com' => 'pl', | |
'grayling.ro' => 'ro', | |
'ro.grayling.com' => 'ro', | |
'rs.grayling.com' => 'ru', | |
'grayling.rs' => 'rs', | |
'grayling.sg' => 'sg', | |
'grayling.sk' => 'sk', | |
'sk.grayling.com' => 'sk', | |
'grayling.si' => 'si', | |
'grayling.es' => 'es', | |
'es.grayling.com' => 'es', | |
'grayling.se' => 'se', | |
'se.grayling.com' => 'se', | |
'grayling.ch' => 'ch', | |
'ch.grayling.com' => 'ch', | |
'trimedia.ch' => 'ch', | |
'tr.grayling.com' => 'tr', | |
'grayling.co.uk' => 'gb', | |
'uk.grayling.com' => 'gb', | |
'us.grayling.com' => 'us', | |
]; | |
/** | |
* Redirect incoming domain requests to the correct | |
* URI on the grayling.com domain. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @param \Closure $next | |
* @return mixed | |
*/ | |
public function handle($request, Closure $next) | |
{ | |
$domain = $this->getDomain($request); | |
$requestedDomain = "{$domain->subdomain}.{$domain->name}.{$domain->tld}"; | |
$redirectDomain = 'website-grayling.cloudapp.net'; | |
if ($domain->subdomain === 'www' || is_null($domain->subdomain)) { | |
$requestedDomain = "{$domain->name}.{$domain->tld}"; | |
} | |
if (env('APP_ENV') === 'local' || $requestedDomain === $redirectDomain) { | |
return $next($request); | |
} | |
if (isset($this->redirects[$requestedDomain])) { | |
return redirect("http://{$redirectDomain}/{$this->redirects[$requestedDomain]}"); | |
} | |
return redirect("http://{$redirectDomain}"); | |
} | |
/** | |
* Get the current domain from the request. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return object | |
*/ | |
private function getDomain($request) | |
{ | |
$parts = explode('.', $request->getHost()); | |
$domain = new stdClass(); | |
$domain->subdomain = isset($parts[0]) ? $parts[0] : null; | |
$domain->name = isset($parts[1]) ? $parts[1] : null; | |
$domain->tld = isset($parts[2]) ? $parts[2] : null; | |
if (count($parts) === 2) { | |
$domain->subdomain = null; | |
$domain->name = $parts[0]; | |
$domain->tld = $parts[1]; | |
} | |
return $domain; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment