Created
April 12, 2012 00:42
-
-
Save webbj74/2363793 to your computer and use it in GitHub Desktop.
Drupal Default Site Redirection Template
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 | |
/** | |
* Redirection rules for default Drupal site | |
* | |
* This drop-in for the default site's settings.php provides HTTP 301 | |
* redirection rules for any request which does not already have a Drupal | |
* site folder assigned. Redirect rules are located in the global | |
* default_redirect array. | |
* | |
* Redirects are selected in the following manner: | |
* 1. HTTP_HOST and REQUEST_URI combined to look for an exact match in | |
* default_redirect | |
* 2. If not found, look for the full HTTP_HOST in default_redirect | |
* 3. Then iterate through progressively more abstract hostname parts until | |
* match is found. | |
* 4. Default site redirect is used if no other is found. | |
* | |
* Example Request: http://aa.bb.example.localhost/some/path | |
* | |
* - looks for match to 'aa.bb.example.localhost/some/path' | |
* - looks for match to 'aa.bb.example.localhost' | |
* - looks for match to 'bb.example.localhost' | |
* - looks for match to 'example.localhost' | |
* - looks for match to 'localhost' | |
* | |
* To use this file to redirect traffic, append or include this file at the | |
* bottom of sites/default/settings.php. For example: | |
* | |
* require_once('default-site-redirect.settings.php'); | |
* | |
* Edit the following defined variables as needed. | |
* | |
* DEFAULT_SITE_REDIRECT - if this is blank, the default drupal site will load | |
* DEFAULT_SITE_DRY_RUN - when true, adds mock-headers instead of redirecting | |
* | |
* Note: in all cases, the SERVER_PORT is dropped from the request's HTTP_HOST | |
*/ | |
define('DEFAULT_SITE_REDIRECT', ''); | |
define('DEFAULT_SITE_DRY_RUN', TRUE); | |
$GLOBALS['default_redirect'] = array ( | |
'b.cc.example.localhost/a-specific-url.html' => 'example-destination.localhost:8082/redirect/rule/0', | |
'aa.bb.cc.example.localhost' => 'example-destination.localhost:8082/redirect/rule/1', | |
'bb.cc.example.localhost' => 'example-destination.localhost:8082/redirect/rule/2', | |
'cc.example.localhost' => 'example-destination.localhost:8082/redirect/rule/3', | |
'example.localhost' => 'example-destination.localhost:8082/redirect/rule/4', | |
); | |
/** | |
* Select the most specific matching pattern for the given hostname | |
*/ | |
function default_site_select_redirect() { | |
global $default_redirect; | |
$location = DEFAULT_SITE_REDIRECT; | |
// Sanitize and trim the HTTP_HOST | |
$http_host = preg_match('/^([a-zA-Z0-9_\-\.]+)(\.)?(:[0-9]+)?$/',$_SERVER['HTTP_HOST'], $matches) ? $matches[1] : 'default'; | |
// Lookup the exact request, incuding URI | |
$request = $http_host . $_SERVER['REQUEST_URI']; | |
if (isset($default_redirect[$request])) { | |
$location = $default_redirect[$request]; | |
} | |
// Iterate through progressively more abstract hostnames | |
else { | |
$server = explode('.', $http_host); | |
for ($j = count($server); $j > 0; $j--) { | |
$pattern = implode('.', array_slice($server, -$j)); | |
// Lookup the partial request, break on the first match | |
if (isset($default_redirect[$pattern])) { | |
$location = $default_redirect[$pattern]; | |
break; | |
} | |
} | |
} | |
return $location; | |
} | |
/** | |
* Handle default site requests | |
*/ | |
function default_site_request_handler() { | |
if ($location = default_site_select_redirect()) { | |
if (!DEFAULT_SITE_DRY_RUN) { | |
drupal_add_http_header('Location', 'http://' . $location); | |
drupal_add_http_header('Status', '301 Moved Permanently'); | |
exit; | |
} | |
else { | |
drupal_add_http_header('X-Dry-Run-Location', 'http://' . $location); | |
drupal_add_http_header('X-Dry-Run-Status', '301 Moved Permanently'); | |
} | |
} | |
} | |
default_site_request_handler(); |
Constants cannot be empty.
The current code causes a PHP notice and in some cases can cause a fatal error.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
may need something like drupal_add_http_header('Cache-Control', 'public, max-age=1209600'); for some varnish implementations