Toggle the "www" domain prefix without .htaccess directives

Two years back I created a basic module toggle_www to toggle the "www" part of your Drupal website, taking the user the correct requested page. In other words, redirect users from http://www.example.com/some/deep/page to http://example.com/some/deep/page or vice-versa. However, killes suggested to take it off, because the same functionality could be achieved using some redirect directives in .htaccess. Being new in the developer community at that time, upon a short thought I took the project page off.

However, users using IIS, or whose servers don't allow .htaccess files - just like this blog's host HCoop.net does(they have their own tool Domtool), need a separate solution if they want to manage the above said. So before I revive the toggle_www module for Drupal 6.x, some overview how we redirect using PHP's header() function. This is rather some easy PHP stuff, with some aspects covered.

From www.example.com to example.com:

  if (!strstr($_SERVER['HTTP_HOST'], 'www.'))
    // if its already example.com, take no action
    return;
 
  // else pass permanently moved info into header and redirect to example.com
  header('HTTP/1.1 301 Moved Permanently');
  header('Location:http://'. substr($_SERVER['HTTP_HOST'], 4) . $_SERVER['REQUEST_URI']);


And from example.com to www.example.com:
  if (strstr($_SERVER['HTTP_HOST'], 'www.'))
    // if it's already with www, take no action
    return;
 
  // else pass permanently moved info into header and redirect to page /w www
  header('HTTP/1.1 301 Moved Permanently');
  header('Location:http://www.'. substr($_SERVER['HTTP_HOST'], 0) . $_SERVER['REQUEST_URI']);


Other concerns:

Maintain a secure connection(HTTPS), if any:
  if ($_SERVER['HTTPS'] == 'on' || $_SERVER['SERVER_PORT'] == 443) {
    // Maintain a secure connection protocol if HTTPS is set "on" or the port is 443.
    $protocol = 'https';
  }
  else {
    // Normal connection protocol.
    $protocol = 'http';
  }

We pass the $protocol variable to header(), to redirect to the correct URL.

Moreover, non-Apache servers and maybe some other configurations miss the $_SERVER['REQUEST_URI']. To cope up with this, we can fill the variable ourselves:
  if (!isset($_SERVER['REQUEST_URI'])) {
      $_SERVER['REQUEST_URI'] = substr($_SERVER['argv'][0], strpos($_SERVER['argv'][0], ';') + 1);
  }


However, Drupal users are lucky enough to be provided with request_uri function to provide the same functionality.

To complete the module, we'll merge the secure protocol check, and the two redirect methods. i.e. with and without "www" part. A basic configuration to select the method would be handy and do the job "pretty" way.

Completing the module with the settings form:
// Fire the toggle check.
if (function_exists('toggle_www_redirect'))  {
  toggle_www_redirect(variable_get('toggle_www_method', 0));
}
 
/**
 * Implementation of hook_menu().
 */
function toggle_www_menu() {
  $items = array();
 
  $items['admin/settings/toggle_www'] = array(
    'title' => t('Toggle WWW'),
    'description' => t('Set the preferred URL style.'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('toggle_www_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('administer site configuration'),
    'type' => MENU_NORMAL_ITEM,
  );
 
  return $items;
}
 
/**
 * Toggle WWW settings form.
 */
function toggle_www_settings() {
  $form = array();
 
  $form['toggle_www_method'] = array(
    '#type' => 'radios',
    '#title' => t('Redirection criteria'),
    '#options' => array(
      t('Disabled - Take 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>')
    ),
    '#default_value' => variable_get('toggle_www_method', 0),
  );
 
  return system_settings_form($form);
}
 
/**
 * toggle_www redirect helper.
 */
function toggle_www_redirect($method = 0) {
  if ($_SERVER['HTTP_HOST'] == 'localhost' || $method == 0) {
  // do not redirect.
    return;
  }
 
  if ($_SERVER['HTTPS'] == 'on' || $_SERVER['SERVER_PORT'] == 443) {
    // Maintain a secure connection protocol.
    $protocol = 'https';
  }
  else {
    // Normal connection protocol.
    $protocol = 'http';
  }
 
  if ($method == 1) {
  // redirect without www part.
    if (!strstr($_SERVER['HTTP_HOST'], 'www.'))
      return;
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: '. $protocol .'://' . substr($_SERVER['HTTP_HOST'], 4) . request_uri());
    return;
  }
  elseif ($method == 2) {
  // redirect with www part.
    if (strstr($_SERVER['HTTP_HOST'], 'www.'))
      return;
    header('HTTP/1.1 301 Moved Permanently');
    header('Location: '. $protocol .'://www.' . substr($_SERVER['HTTP_HOST'], 0) . request_uri());
    return;
  }
}


Download the complete module package, for Drupal 6.2 and onwards, here. I'll also be releasing this version of module on drupal.org project page at http://drupal.org/project/toggle_www.

UPDATE: toggle_www for Drupal 6.x is here: http://drupal.org/node/248354

0 Comments

Post new comment

Smileys
:);):P*JOKINGLY*8)8-|:(:O(pl)]:&lt;O:)&lt;:o):thinking(Y)
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <del> <code> <ul> <ol> <li> <small> <br> <img> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo]. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options