Skip to content

Instantly share code, notes, and snippets.

@wernerkrauss
Created November 30, 2016 13:31
Show Gist options
  • Save wernerkrauss/24bf8fa061909b3e50dadbcc9a597aa0 to your computer and use it in GitHub Desktop.
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)
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();
}
@wernerkrauss
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment