/[drupal]/contributions/modules/toggle_www/toggle_www.module
ViewVC logotype

Contents of /contributions/modules/toggle_www/toggle_www.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Fri Apr 18 17:15:39 2008 UTC (19 months, 1 week ago) by Gurpartap
Branch: MAIN
CVS Tags: DRUPAL-6--1-0, HEAD
Changes since 1.3: +72 -30 lines
File MIME type: text/x-php
An easy, PHP header redirect based way to redirect incoming links from http://www.example.com/some/deep/page to http://example.com/some/deep/page or vice-versa.
1 <?php
2 // $Id: $
3
4 // Fire the toggle check.
5 if (function_exists('toggle_www_redirect')) {
6 toggle_www_redirect(variable_get('toggle_www_method', 0));
7 }
8
9 /**
10 * Implementation of hook_help().
11 */
12 function toggle_www_help($path, $arg) {
13 switch ($path) {
14 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 or vice-versa.');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function toggle_www_menu() {
23 $items = array();
24
25 $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
38 /**
39 * 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;
45 }
46
47 if ($_SERVER['HTTPS'] == 'on' || $_SERVER['SERVER_PORT'] == 443) {
48 // Maintain a secure connection protocol.
49 $protocol = 'https';
50 }
51 else {
52 // 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;
64 }
65 elseif ($method == 2) {
66 // redirect from example.com to www.example.com.
67 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() {
80 $form = array();
81
82 $form['toggle_www_settings'] = array(
83 '#type' => 'fieldset',
84 '#title' => t('Basic configuration'),
85 '#collapsed' => TRUE,
86 );
87 $form['toggle_www_settings']['toggle_www_method'] = array(
88 '#type' => 'radios',
89 '#title' => t('Redirection criteria'),
90 '#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),
96 );
97
98 return system_settings_form($form);
99 }

  ViewVC Help
Powered by ViewVC 1.1.2