Created
November 30, 2016 13:31
-
-
Save wernerkrauss/24bf8fa061909b3e50dadbcc9a597aa0 to your computer and use it in GitHub Desktop.
redirect to main domain (e.g. from domain.com.host.com to domain.com)
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
if (Director::isLive()) { | |
$hostname = gethostname(); | |
$http_host = $_SERVER['HTTP_HOST']; | |
if (strpos($http_host, $hostname) !== false) { | |
//redirect to original domain | |
$destURL = str_replace('.' . $hostname, '', $http_host); | |
$response = new SS_HTTPResponse(); | |
$response->redirect('http://' . $destURL, 301); | |
HTTP::add_cache_headers($response); | |
// TODO: Use an exception - ATM we can be called from _config.php, before Director#handleRequest's try block | |
$response->output(); | |
die; | |
} | |
Director::forceSSL(); | |
Director::forceWWW(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This redirects e.g. on a hetzner managed server, where every account has a subdomain of the hostname, e.g. example.com.dedi1234.your-server.de which should redirect to example.com in live.
Used together with some SilverStripe methods to force ssl and www subdomain.