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

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.

use .htaccess

reikiman - July 3, 2007 - 20:35

This is much simpler with .htaccess:

Redirect permanent / http://www.domain.example

But it does require having a separate docroot for www.domain.example and domain.example

- David Herron - http://7gen.com/

Using .htaccess

shadyman@errora... - September 15, 2007 - 02:49

Even easier, as far as redirects go is to use mod_rewrite. This is the way Drupal suggests it:

<IfModule mod_rewrite.c>
  RewriteEngine on

  # If your site can be accessed both with and without the prefix www. you
  # can use one of the following settings to force user to use only one option:
  #
  # If you want the site to be accessed WITH the www. only, adapt and
  # uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule .* http://www.example.com/ [L,R=301]
  #
  # If you want the site to be accessed only WITHOUT the www. prefix, adapt
  # and uncomment the following:
  #RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  #RewriteRule .* http://example.com/ [L,R=301]

  # Probably some other stuff

</IfModule>

Just uncomment one of the two sets of lines of RewriteCond/RewriteRule, depending if you want your site to always show as www.example.com or example.com.

Remember to replace example.com with your domain name.

You will lose the rest of

anoopjohn - January 2, 2008 - 19:46

You will lose the rest of the URL if you run the rewrite rule mentioned above
The above URL rewrite will result in
http://example.com/somepath/otherpath redirected to http://www.example.com

Use

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.+) http://www.example.com/$1 [L,R=301]

to redirect with the rest of the URL intact.
This will correctly redirect
http://example.com/somepath/otherpath to http://www.example.com/somepath/otherpath

The difference is the .* pattern in the rewrite condition replaced with (.+) and the $1 at the end of the URL.
If instead you want to use the non-www url as your standard url apply the same concept in the second rewrite url also.

Cheers
Anoop

http://www.zyxware.com
=========================
Be the change you wish to see in the world
M. K. Gandhi
=========================

 
 

Drupal is a registered trademark of Dries Buytaert.