Access all multisites with www. only [.htaccess]

Last modified: February 29, 2008 - 16:31

The default .htaccess RewriteRule to redirect every multisite.com to www.multisite.com does not work properly.

If you have problems with that too, just leave the two lines uncommented, and ADD THE FOLLOWING 3 LINES FOR EVERY MULTISITE you set up:

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

(Thanks to Florian!)

Redirect multiple domains to a canonical domain

pnm - January 19, 2007 - 23:21

Also, to redirect examples.com and www.examples.com to www.example.com, use these lines:

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

This is useful in an .htaccess file if example.com and examples.com share the same document root.

A more generic rewrite rule for 'non www' to 'with www'

open-keywords - April 8, 2008 - 08:45

Instead of requiring to edit the .htaccess file for every domain being added to a multi-site setup, I suggest the following set of rewrite rules for a generic solution:

# non empty HTTP_HOST in the request
        RewriteCond %{HTTP_HOST} !^$ [NC]
# does not start with 'www'
        RewriteCond %{HTTP_HOST} !^www\. [NC]
# saves the value of HTTP_HOST in %1
        RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
# redirects using the saved URI (in RewriteRule) and saved hostname (in the last RewriteCond)
        RewriteRule ^(.*)$ "http://www.%1/$1" [L,R=301]
# that's it !

It works for every domain name
Tested with Apache 2.2.3

Opposite requirement: remove 'www', run all sites without 'www'

open-keywords - September 22, 2008 - 07:31

If you need the opposite behavior, I propose the following:

# non empty HTTP_HOST in the request
        RewriteCond %{HTTP_HOST} !^$ [NC]
# the hostname does start with 'www.'
        RewriteCond %{HTTP_HOST} ^www\. [NC]
# let's extract the hostname without 'www.' and save it to %1
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# let's redirect to the extracted hostname, $1 saves the URL
        RewriteRule ^(.*)$ "http://%1/$1" [L,R=301]

In both cases, these rules won't be enough if you run on HTTPS or another server port than the default 80 (but it would certainly be possible to enhance them to support these situations)

Feedback welcome

it works, but not for the default site

no2e - December 9, 2008 - 22:24

(I described my problem in the multisite group, but got no replies)

I tested your code (for www to non-www redirection) inside of my unmodified .htaccess (I pasted it behind the commented www/non-www block)

# non empty HTTP_HOST in the request
        RewriteCond %{HTTP_HOST} !^$ [NC]
# the hostname does start with 'www.'
        RewriteCond %{HTTP_HOST} ^www\. [NC]
# let's extract the hostname without 'www.' and save it to %1
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# let's redirect to the extracted hostname, $1 saves the URL
        RewriteRule ^(.*)$ "http://%1/$1" [L,R=301]

Now, it works great for my multisites.
But when I try to call my default (main) site, I get redirected wrong:

Drupal is installed at http://example.org/drupal. When I call it, I get redirected to http://example.org/ :-( (where no Drupal is used -> static website)

What do I have to do, please? I don't absolute need www-to-non-www redirection for my main site (but for all of my multisites). I just want my default site to be accessible ...

(bump)

no2e - December 13, 2008 - 02:17

any help, please? :|

bump²

no2e - December 14, 2008 - 18:42

got this problem 13 days now and can't start with Drupal, until I know, that it is possible to solve. Any help, please?

.

no2e - December 16, 2008 - 19:04

I'll update to 6.8 (from 6.6) in the next hours, but I don't think it'll fix the problem.
Any help, please? I want to play with Drupal ... :|

need help

no2e - December 20, 2008 - 01:05

(bump)

Perhaps Try... RewriteBase

QuangVan - June 2, 2009 - 12:58

Put a RewriteBase /drupal in front of your RewriteCond/RewriteRule

This is what is said in Drupal's htaccess

# Modify the RewriteBase if you are using Drupal in a subdirectory or in a
# VirtualDocumentRoot and the rewrite rules are not working properly.
# For example if your site is at http://example.com/drupal uncomment and
# modify the following line:
# RewriteBase /drupal
#
# If your site is running in a VirtualDocumentRoot at http://example.com/,
# uncomment the following line:
#RewriteBase /

# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.

So your .htaccess should contain

  RewriteBase /drupal
  RewriteCond %{HTTP_HOST} !^$ [NC]
  RewriteCond %{HTTP_HOST} ^www\. [NC]
  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ "http://%1/$1" [L,R=301]

Not sure if it'll work but worth a try...

On another note my personal .htaccess rewrite rules looks like: (my drupal is installed in my root, ~/www/)

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

  #Rewrites http://www.drupal-site.com/story/the-title -> http://drupal-site.com/story/the-title
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

  #Rewrites http://www.non-drupal-site.com/story/the-title -> http://non-drupal-site.com/story/the-title
  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
  RewriteRule ^(.*)$ http://$1 [R=301,L]


  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

I was having some problems with getting rid of www on both drupal sites and non-drupal sites, hence the long-winded solution above, if anyone have other suggestions please advise.

http://no-www.org/faq.php Giv

QuangVan - July 28, 2009 - 19:41

http://no-www.org/faq.php

Give's the configuation

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

How do we do this if we want

kalashari - March 19, 2009 - 00:26

How do we do this if we want to have this rule for all sites except one site where we want to have www to no-www ?

Thanks for all the solutions

marcopolo - June 4, 2009 - 06:09

Thanks for all the solutions above...

I also want to find out the solution for this one...

Can you just use this one...

# non empty HTTP_HOST in the request
        RewriteCond %{HTTP_HOST} !^$ [NC]
# the hostname does start with 'www.'
        RewriteCond %{HTTP_HOST} ^www\. [NC]
# let's extract the hostname without 'www.' and save it to %1
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# let's redirect to the extracted hostname, $1 saves the URL
        RewriteRule ^(.*)$ "http://%1/$1" [L,R=301]

... and then add this one at the end for the one with www...

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

Would that work or will it stuff everything up?

Handling specific redirect for a given site

open-keywords - July 1, 2009 - 05:38

It should work, if you put it above the generic rule and not below.

It should also be possible to insert an exception rule in second position, like this

# non empty HTTP_HOST in the request
        RewriteCond %{HTTP_HOST} !^$ [NC]
# not a given host HTTP_HOST in the request if you have an opposite requirement for a given host name
RewriteCond %{HTTP_HOST} !exception\.com$ [NC]
# the hostname does start with 'www.'
        RewriteCond %{HTTP_HOST} ^www\. [NC]
# let's extract the hostname without 'www.' and save it to %1
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# let's redirect to the extracted hostname, $1 saves the URL
        RewriteRule ^(.*)$ "http://%1/$1" [L,R=301]

Hope this helps

Exception that works

portulaca - July 2, 2009 - 15:01

open-keywords neither of your suggestions work, putting the usual code for the domain to be excepted before the universal rules doesn't work, and neither the second option to put an exception as a rule in the "second line".

I have several multisites that all go to no www, but I have one that has to be with www. Here is what I use (and it works), maybe someone else can make it more elegant (I added the parts marked in bold):

# non empty HTTP_HOST in the request
        RewriteCond %{HTTP_HOST} !^$ [NC]  

#here you exclude a domain from the universal rules
        RewriteCond %{HTTP_HOST} !^www\.excepteddomain\.com$ [NC]
        RewriteCond %{HTTP_HOST} !^excepteddomain\.com$ [NC]

# the hostname does start with 'www.'
        RewriteCond %{HTTP_HOST} ^www\. [NC]
# let's extract the hostname without 'www.' and save it to %1
        RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
# let's redirect to the extracted hostname, $1 saves the URL
        RewriteRule ^(.*)$ "http://%1/$1" [L,R=301]

#here are the rules for the excepted domain
       RewriteCond %{HTTP_HOST} excepteddomain\.com$ [NC]
       RewriteCond %{HTTP_HOST} !^www\.excepteddomain\.com$ [NC]
       RewriteRule ^(.*)$ http://www.excepteddomain.com/$1 [L,R=301]

 
 

Drupal is a registered trademark of Dries Buytaert.