Skip to content

Instantly share code, notes, and snippets.

@ilyautkin
Created August 28, 2019 08:39
Show Gist options
  • Save ilyautkin/ed4aaa40ecb1b149bdf0aba4b0527789 to your computer and use it in GitHub Desktop.
Save ilyautkin/ed4aaa40ecb1b149bdf0aba4b0527789 to your computer and use it in GitHub Desktop.
<?php
if ($modx->event->name != "OnHandleRequest" || $modx->context->key == 'mgr') {
return;
}
$tmp = explode('?', $_SERVER['REQUEST_URI']);
$link = trim($tmp[0], '/');
if (isset($tmp[1]) && $tmp[1]) {
$params = '?' . $tmp[1];
} else {
$params = '';
}
$uri = '/' . strtolower($link) . $params;
$http_host = $_SERVER['HTTP_HOST'];
$site_url = str_replace('www.', '', $_SERVER['HTTP_HOST']);
// for https set true
$https = true;
if ($https) {
$protocol = 'https://';
} else {
$protocol = 'http://';
}
if ($http_host != $site_url || ($https && !$_SERVER['HTTPS']) || $_SERVER['REQUEST_URI'] != $uri) {
$modx->sendRedirect($protocol.$site_url.$uri, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
}
if ($_SERVER['REQUEST_URI'] == '/index.php') {
$modx->sendRedirect($protocol.$site_url, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
}
$cityFields = $modx->getService('cityfields', 'cityFields', MODX_CORE_PATH.'components/cityfields/model/cityfields/', []);
$tmp = explode('.', $http_host);
$zone = array_pop($tmp);
$first = array_pop($tmp);
$subdomain = implode('.', $tmp);
if ($subdomain) {
if ($city = $modx->getObject('cfCity', ['key' => $subdomain, 'active' => true])) {
if ($modx->getOption('cityfields_default_city', null, 1) == $city->id) {
$modx->sendRedirect($protocol.$site_url.$uri, array('responseCode' => 'HTTP/1.1 301 Moved Permanently'));
}
} else {
header("HTTP/1.0 404 Not found");
die();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment