run(); Class staticRedirector { private $config = array( /** * http://sub1.domain.com => http://domain.com * https://sub1.domain.com => https://domain.com * http://www.domain.com => http://domain.com * https://www.domain.com => https://domain.com * http://domain.com => http://www1.domain.com * https://domain.com => https://www1.domain.com */ 'subDomainRedirects' => array( 'http://www.torrents3d.ru' => 'http://torrents3d.ru', // 'http://www.sandbox.local' => 'http://sub.sandbox.local/dsfds', ), /** * (bool) trailing slash [true|false] */ 'trailingSlash' => true, /** * /url.html => /anotherPage.html * /category/ => http://domain.com */ 'customRedirects' => array( // '/url1.html' => '/url2.html', // '/redirect.php' => 'http://example.com', ), /** * Replace in content * 'text1' => 'text2' */ 'replaceInContent' => array( // 'example' => 'sdfdsf', // 'lovecity' => '111', ), ); const SCHEME_DELIMITER = '://'; const CONTENT_DIRECTORY = ''; const DOCUMENT_404 = '/404.html'; const DOCUMENT_MAIN_HTML = '/main.html'; const DOCUMENT_MAIN_PHP = '/main.php'; public function __construct() { } public function run() { /* * subDomainRedirects */ $this->subDomainRedirects(); /* * trailingSlash */ $this->trailingSlash(); /* * customRedirects */ $this->customRedirects(); /* * draw Content or 404 */ $this->drawStaticContent(); } private function subDomainRedirects() { if (!isset($this->config['subDomainRedirects'])) { return; } $httpHost = $_SERVER['HTTP_HOST']; $schemeWithHost = $this->getProtocol() . self::SCHEME_DELIMITER . $httpHost; if (in_array($schemeWithHost, array_keys($this->config['subDomainRedirects']))) { $this->redirect($this->config['subDomainRedirects'][$schemeWithHost]); } } private function trailingSlash() { if (!isset($this->config['trailingSlash'])) { return; } $REQUEST_URI = $this->getRequestUri(); if ( $REQUEST_URI == '/' || preg_match('/\.html$/', $REQUEST_URI) || preg_match('/\.php/', $REQUEST_URI) ) { return; } if ( $this->config['trailingSlash'] == false && preg_match('/\/$/', $REQUEST_URI) ) { $this->redirect(preg_replace('/\/$/', '', $REQUEST_URI)); } if ( $this->config['trailingSlash'] == true && !preg_match('/\/$/', $REQUEST_URI) ) { $this->redirect($REQUEST_URI . '/'); } } private function customRedirects() { if (!isset($this->config['customRedirects'])) { return; } $REQUEST_URI = $this->getRequestUri(); if ($REQUEST_URI == '/') { return; } if (in_array($REQUEST_URI, array_keys($this->config['customRedirects']))) { $this->redirect($this->config['customRedirects'][$REQUEST_URI]); } } private function redirect($location) { $header = sprintf('Location: %s', trim($location)); header($header); exit(); } private function getRequestUri() { return strtok($_SERVER["REQUEST_URI"], '?'); } private function getProtocol() { if (isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { return 'https'; } else { return 'http'; } } private function drawStaticContent() { $content = NULL; $REQUEST_URI = $this->getRequestUri(); try { // main.html if ( $REQUEST_URI == '/' && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . self::DOCUMENT_MAIN_HTML) ) { throw new Exception(dirname(__FILE__) . self::CONTENT_DIRECTORY . self::DOCUMENT_MAIN_HTML); } // main.php if ( $REQUEST_URI == '/' && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . self::DOCUMENT_MAIN_PHP) ) { throw new Exception(dirname(__FILE__) . self::CONTENT_DIRECTORY . self::DOCUMENT_MAIN_PHP); } // /file.html if ( preg_match('/\.html$/', $REQUEST_URI) && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI) ) { throw new Exception(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI); } // /file.php if ( preg_match('/\.php$/', $REQUEST_URI) && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI) ) { throw new Exception(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI); } $REQUEST_URI_NO_RIGHT_SLASH = rtrim($REQUEST_URI, '/'); if ( !preg_match('/\.html$/', $REQUEST_URI) && !preg_match('/\.php$/', $REQUEST_URI) && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI_NO_RIGHT_SLASH . DIRECTORY_SEPARATOR . 'index.html') ) { throw new Exception(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI_NO_RIGHT_SLASH . DIRECTORY_SEPARATOR . 'index.html'); } if ( !preg_match('/\.html$/', $REQUEST_URI) && !preg_match('/\.php$/', $REQUEST_URI) && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI_NO_RIGHT_SLASH . DIRECTORY_SEPARATOR . 'index.php') ) { throw new Exception(dirname(__FILE__) . self::CONTENT_DIRECTORY . $REQUEST_URI_NO_RIGHT_SLASH . DIRECTORY_SEPARATOR . 'index.php'); } } catch (Exception $foundFile) { $content = file_get_contents($foundFile->getMessage()); } if ( $content === NULL && file_exists(dirname(__FILE__) . self::CONTENT_DIRECTORY . self::DOCUMENT_404) ) { $content = file_get_contents(dirname(__FILE__) . self::CONTENT_DIRECTORY . self::DOCUMENT_404); } if (isset($this->config['replaceInContent'])) { $content = str_ireplace(array_keys($this->config['replaceInContent']), array_values($this->config['replaceInContent']), $content); } echo $content; } }