| 1 |
<?php |
<?php |
| 2 |
|
// $Id: $ |
| 3 |
|
|
| 4 |
|
// Fire the toggle check. |
| 5 |
if (function_exists('toggle_www_redirect')) { |
if (function_exists('toggle_www_redirect')) { |
| 6 |
toggle_www_redirect(variable_get('toggle_www_method', 0)); |
toggle_www_redirect(variable_get('toggle_www_method', 0)); |
| 7 |
} |
} |
| 8 |
|
|
| 9 |
function toggle_www_help($section) { |
/** |
| 10 |
switch ($section) { |
* Implementation of hook_help(). |
| 11 |
|
*/ |
| 12 |
|
function toggle_www_help($path, $arg) { |
| 13 |
|
switch ($path) { |
| 14 |
case 'admin/modules#description': |
case 'admin/modules#description': |
| 15 |
return t('An easy, PHP header redirect based way to redirect incoming links from http://www.example.com to http://example.com and vice-versa.'); |
return t('An easy, PHP header redirect based way to redirect incoming links from http://www.example.com to http://example.com or vice-versa.'); |
| 16 |
} |
} |
| 17 |
} |
} |
| 18 |
|
|
| 19 |
function toggle_www_redirect($method = 0) { |
/** |
| 20 |
if ($_SERVER['HTTP_HOST'] == 'localhost') |
* Implementation of hook_menu(). |
| 21 |
{ |
*/ |
| 22 |
$method = 0; |
function toggle_www_menu() { |
| 23 |
} |
$items = array(); |
| 24 |
if ($method == 0) { |
|
| 25 |
return; |
$items['admin/settings/toggle_www'] = array( |
| 26 |
|
'title' => t('Toggle WWW'), |
| 27 |
|
'description' => t('Set the preferred URL style.'), |
| 28 |
|
'page callback' => 'drupal_get_form', |
| 29 |
|
'page arguments' => array('toggle_www_settings'), |
| 30 |
|
'access callback' => 'user_access', |
| 31 |
|
'access arguments' => array('administer site configuration'), |
| 32 |
|
'type' => MENU_NORMAL_ITEM, |
| 33 |
|
); |
| 34 |
|
|
| 35 |
|
return $items; |
| 36 |
} |
} |
| 37 |
// $method = 1 means redirect from www.example.com to example.com |
|
| 38 |
if ($method == 1) { |
/** |
| 39 |
if ( !strstr( $_SERVER['HTTP_HOST'], 'www.' )) // if its already example.com, take no action |
* toggle_www redirect helper. |
| 40 |
|
*/ |
| 41 |
|
function toggle_www_redirect($method = 0) { |
| 42 |
|
if ($_SERVER['HTTP_HOST'] == 'localhost' || $method == 0) { |
| 43 |
|
// do not redirect. |
| 44 |
return; |
return; |
| 45 |
// otherwise pass permanently moved information into header and redirect to example.com |
} |
| 46 |
header('HTTP/1.1 301 Moved Permanently'); |
|
| 47 |
header('Location: http://' . substr($_SERVER['HTTP_HOST'], 4) . $_SERVER['REQUEST_URI']); |
if ($_SERVER['HTTPS'] == 'on' || $_SERVER['SERVER_PORT'] == 443) { |
| 48 |
return; |
// Maintain a secure connection protocol. |
| 49 |
} |
$protocol = 'https'; |
| 50 |
// $method = 2 means redirect from example.com to www.example.com |
} |
| 51 |
elseif ($method == 2) { |
else { |
| 52 |
if ( strstr( $_SERVER['HTTP_HOST'], 'www.' )) // if its already www.example.com, take no action |
// Normal connection protocol. |
| 53 |
|
$protocol = 'http'; |
| 54 |
|
} |
| 55 |
|
|
| 56 |
|
if ($method == 1) { |
| 57 |
|
// redirect from www.example.com to example.com. |
| 58 |
|
if (!strstr($_SERVER['HTTP_HOST'], 'www.')) // if its already example.com, take no action |
| 59 |
|
return; |
| 60 |
|
// otherwise pass permanently moved information into header and redirect to example.com |
| 61 |
|
header('HTTP/1.1 301 Moved Permanently'); |
| 62 |
|
header('Location: '. $protocol .'://' . substr($_SERVER['HTTP_HOST'], 4) . request_uri()); |
| 63 |
return; |
return; |
| 64 |
// otherwise pass permanently moved information into header and redirect to www.example.com |
} |
| 65 |
header('HTTP/1.1 301 Moved Permanently'); |
elseif ($method == 2) { |
| 66 |
header('Location: http://www.' . substr($_SERVER['HTTP_HOST'], 0) . $_SERVER['REQUEST_URI']); |
// redirect from example.com to www.example.com. |
| 67 |
return; |
if (strstr($_SERVER['HTTP_HOST'], 'www.')) // if its already www.example.com, take no action |
| 68 |
} |
return; |
| 69 |
|
// otherwise pass permanently moved information into header and redirect to www.example.com |
| 70 |
|
header('HTTP/1.1 301 Moved Permanently'); |
| 71 |
|
header('Location: '. $protocol .'://www.' . substr($_SERVER['HTTP_HOST'], 0) . request_uri()); |
| 72 |
|
return; |
| 73 |
|
} |
| 74 |
} |
} |
| 75 |
|
|
| 76 |
|
/** |
| 77 |
|
* Toggle WWW settings form. |
| 78 |
|
*/ |
| 79 |
function toggle_www_settings() { |
function toggle_www_settings() { |
| 80 |
$form = array(); |
$form = array(); |
| 81 |
|
|
| 82 |
$form['toggle_www_settings'] = array( |
$form['toggle_www_settings'] = array( |
| 83 |
'#type' => 'fieldset', |
'#type' => 'fieldset', |
| 84 |
|
'#title' => t('Basic configuration'), |
| 85 |
'#collapsed' => TRUE, |
'#collapsed' => TRUE, |
| 86 |
); |
); |
| 87 |
$form['toggle_www_settings']['toggle_www_method'] = array( |
$form['toggle_www_settings']['toggle_www_method'] = array( |
| 88 |
'#type' => 'radios', |
'#type' => 'radios', |
| 89 |
'#title' => t('Redirection criteria'), |
'#title' => t('Redirection criteria'), |
| 90 |
'#options' => array( t('Inactive - Module takes no action.'), t('No "www" - Redirect from <em>www.example.com</em> to <em>example.com</em>'), t('Always add "www" - Redirect from <em>example.com</em> to <em>www.example.com</em>')), |
'#options' => array( |
| 91 |
|
t('Disabled - Take no action.'), |
| 92 |
|
t('No "www" - Redirect from <em>www.example.com</em> to <em>example.com</em>'), |
| 93 |
|
t('Always add "www" - Redirect from <em>example.com</em> to <em>www.example.com</em>') |
| 94 |
|
), |
| 95 |
'#default_value' => variable_get('toggle_www_method', 0), |
'#default_value' => variable_get('toggle_www_method', 0), |
| 96 |
); |
); |
| 97 |
|
|
| 98 |
return $form; |
return system_settings_form($form); |
| 99 |
} |
} |