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
Also, to redirect examples.com and www.examples.com to www.example.com, use these lines:
RewriteCond %{HTTP_HOST} examples\.comRewriteRule (.*) 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'
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 requestRewriteCond %{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'
If you need the opposite behavior, I propose the following:
# non empty HTTP_HOST in the requestRewriteCond %{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
(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 requestRewriteCond %{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)
any help, please? :|
bump²
got this problem 13 days now and can't start with Drupal, until I know, that it is possible to solve. Any help, please?
.
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
(bump)
Perhaps Try... RewriteBase
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 /drupalRewriteCond %{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.
Wealth Programming
http://no-www.org/faq.php Giv
http://no-www.org/faq.php
Give's the configuation
RewriteEngine OnRewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Wealth Programming
How do we do this if we want
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
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 requestRewriteCond %{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?
marcopolo ---
Blog Marco | Best Designed Drupal Websites
Handling specific redirect for a given site
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 requestRewriteCond %{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
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 requestRewriteCond %{HTTP_HOST} !^$ [NC]
#here you exclude a domain from the universal rulesRewriteCond %{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 domainRewriteCond %{HTTP_HOST} excepteddomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.excepteddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.excepteddomain.com/$1 [L,R=301]