Mod Rewrite for Multiple Drupal Sites
I have a sister, who is a genius on anything unix based... She set me up with a .htaccess file that allows me to host two Drupal sites, each in their own folder. I did some extensive searching for such code, and found absolutely NOTHING that outlined something as simple as pointing domaina.com to folder /h/domaina.com, and domainb.com to folder /h/domainb.com. I tried all sorts of configurations but ultimately was unsuccessful.
I use a hosting provider that uses cPanel which is a linux based web hosting management control panel. Being linux based, it uses apache with mod_rewrite and further access to .htaccess files. (I don't know that much about linux, apache, etc... sorry if my terminology is poor) Anyway, I have two different code sets. The first set is for a site with multiple Drupal installs / configurations. the .htaccess goes into the root of the public_html folder (~homedirectory/public_html/.htaccess) and directs which folder to go into.
The cool thing about the code: it is setup to be as generic as possible, which allows for just about any combination of domain's.
I have replaced my specific domain info with [your domain] and my folders with [your folder].
The section that requires this info, "might" be required to set the primary domain information for proper redirecting. For example, I have xenitive.com as my primary domain and another domain setup as a 'mirrored' domain. I had to set xenitive.com up to be the 'primary' domain in order for the redirect to work. There is a folder rule that is commented out... I'm not sure what it's there for exactly...
I personally have everything setup in "public_html/h/[my domains]" but i have removed the /h/ from the code.
This can be added in at:
# serve domain specific content
RewriteRule ^ http://%{HTTP_HOST}/h/%{HTTP_HOST}%{REQUEST_URI} [L]
# $Id: .htaccess,v 1.6 2007/07/20 15:23:46 philswar state $
# -FrontPage-
# [your domain]
Options +FollowSymLinks
IndexIgnore .htaccess */.??* *~ *#
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# handle default domain -- [your domain]
RewriteCond %{HTTP_HOST} ^$
RewriteRule ^ http://[your domain]%{REQUEST_URI} [L,R]
# RewriteRule !^[your folder]/.*$ /$1 [L,R=301]
# serve domain specific content
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^ http://%{HTTP_HOST}/%{HTTP_HOST}%{REQUEST_URI} [L]
# pull off leading www. from hostname
RewriteCond %{HTTP_HOST} ^www.(.*)
RewriteRule ^ http://%1%{REQUEST_URI} [L,R]
#------------------------------------------------------------------------------
# other stuff
#
# add www. to hostname
#RewriteCond %{HTTP_HOST} !^www. [NC]
#RewriteCond %{HTTP_HOST} !^$
#RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]Ok, the second set of code I want to address, is for a single Drupal site, and the Drupal specific .htaccess file. I pull off the leading www since it really isn't necessary and technically points to a subdomain, except that www for most sites is the actual domain. It has improperly been taught that way over the years, so whatever...
If you want to experiment with the code above to ADD the www, be my guest, but I haven't personally tried it.
Find the following code in the Drupal .htaccess file:
# 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]Instead of rewriting the commented:
# RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
# RewriteRule .* http://example.com/ [L,R=301]I would recommend just adding the following code below the commented lines:
RewriteCond %{HTTP_HOST} ^www.(.*)
RewriteRule ^ http://%1%{REQUEST_URI} [L,R]Basically, so it looks like this:
# 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]
RewriteCond %{HTTP_HOST} ^www.(.*)
RewriteRule ^ http://%1%{REQUEST_URI} [L,R]All done!
The nice feature to this code is that it allows you to create a Drupal site template, and you don't have to go edit the .htaccess for each new Drupal site installation.
If you want to comment, feel free. If you need help... I'm afraid I won't be of much assistance... I don't fully understand the apache mod_rewrite command syntax...
