OUTDATED: 301 Redirect from domain.com to www.domain.com

Last modified: September 7, 2008 - 23:44

NOTE: using .htaccess rules is a much better way to accomplish this goal.

I needed to use 301 to redirect all non-www requests for a site to the www version and here's how I did it. I made two folders in the sites folder:

sites/www.domain.com/settings.php
sites/domain.com/settings.php

I then put the following in the non-www (domain.com) settings.php file. This is the entire contents of the file:

<?php
  $host
= $GLOBALS['HTTP_SERVER_VARS']['HTTP_HOST'];
 
$request = $GLOBALS['HTTP_SERVER_VARS']['REQUEST_URI'];
 
header("HTTP/1.0 301 Moved Permanently");
 
header("Location: http://www.$host$request");
 
header("Connection: close");
?>

I would rather do this with mod_rewrite, but it looks complex and I don't have time to study that right now. If anybody has a suggestion that would work in a multisite environment, that'd be really great.

 
 

Drupal is a registered trademark of Dries Buytaert.