[SOLVED]Multiple Virtual Hosts and Aliases

Using a single web hosting account to host multiple sites

[SOLVED]Multiple Virtual Hosts and Aliases

Postby jasperroel » Wed Jan 16, 2008 2:00 am

I have a website in place in one directory (say /var/www/website) which handles multiple domains and aliases.

For example, it handles

www.example.com
www.example.net
www.example.org/front

On a server, it does this for about 80 sites.

The problem is that if a virtual host has an Alias to my website (say www.example.org/front) and the DocumentRoot is set to something else then /var/www/website (like /var/www/static_content) RewriteRules will not work anymore (because it wil look in /var/www/static_content instead of /var/www/website).

Normally a "RewriteBase /front" should fix this, but then I'd break the websites which do not have a "Alias /front /var/www/website" in place.

A "RewriteBase /" is also not working if the site has only Aliasses pointing to /var/www/website.

Is there any way on setting this dynamicly of finding out which Alias was used, so we can do a "RewriteBase /%{alias}" or something?

From an old post, I already have this

# Only do the rewriting if rewriting is enabled
<IfModule mod_rewrite.c>
Options +FollowSymLinks

RewriteEngine On

# Make sure it starts with home. - means do nothing (carry on to the next rules)
# E=BASE:%1 sets the %{ENV:BASE} variable to the bit before home/
RewriteRule ^home(/.*)?$ - [NC,E=BASE:%1]
</IfModule>

A bit is snipped out, but couldn't it be possible to use the ENV:BASE variable for RewriteBase (like "RewriteBase %{ENV:BASE}")? Hint: this line doesn't work... (gives a "Internal Server Error - RewriteBase: argument is not a valid URL)

Any help is appreciated!
Last edited by jasperroel on Fri Jan 25, 2008 12:46 am, edited 1 time in total.
jasperroel
 
Posts: 9
Joined: Tue Mar 20, 2007 4:23 am

Postby richardk » Wed Jan 16, 2008 3:23 pm

I have a website in place in one directory (say /var/www/website) which handles multiple domains and aliases.

...

The problem is that if a virtual host has an Alias to my website (say www.example.org/front) and the DocumentRoot is set to something else then /var/www/website (like /var/www/static_content) RewriteRules will not work anymore (because it wil look in /var/www/static_content instead of /var/www/website).

So all the domains have different document roots and all the Aliases go to a single directory?

Where are you putting the mod_rewrite? Is the mod_rewrite only for the Alias? Can you put the mod_rewrite in the httpd.conf file? If you can you can try the PT flag to make normal rules work.

A bit is snipped out, but couldn't it be possible to use the ENV:BASE variable for RewriteBase (like "RewriteBase %{ENV:BASE}")? Hint: this line doesn't work... (gives a "Internal Server Error - RewriteBase: argument is not a valid URL)

You cannot dynamically set the RewriteBase. You could try using it in the RewriteRule, eg.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Set the base variable.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\1$
RewriteRule ^(.*)$ - [E=BASE:%1]

# Rewrite .../abc/123 to .../def/123
RewriteRule ^abc(.*)$ %{ENV:BASE}def$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jasperroel » Mon Jan 21, 2008 2:01 am

Thanks for your reply. I'll try some of your suggestions as soon as possible.

In the meantime some answers to your question.


So all the domains have different document roots and all the Aliases go to a single directory?

No, some domains have a different DocumentRoot, and if an Alias is used for our CMS, is usually points to our CMS (because else the Alias has no meaning to us).

Where are you putting the mod_rewrite? Is the mod_rewrite only for the Alias? Can you put the mod_rewrite in the httpd.conf file? If you can you can try the PT flag to make normal rules work.

I enable the mod_rewrite in the httpd.conf, and have in the rewrite rules themselfes in a .htacces.

The mod_rewrite goes for all requests, domain as well as alias. Every request should pass through.



My config is as follows:

Our content management system: /var/www/cms3-unstable
(.htaccess: /var/www/cms/.htaccess)

Our httpd.conf [vhosts.conf] (simplified):

## This works fine without a RewriteBase ##
<Domain www.ghi.com>
DocumentRoot /var/www/cms3-unstable
# No Aliasses
</Domain>

## This works, because the Root is the same as the Aliasses (so it works throught the main domain) ##
<Domain www.abc.com>
DocumentRoot /var/www/cms3-unstable
Alias /cust_01 /var/www/cms3-unstable
Alias /cust_02 /var/www/cms3-unstable
</Domain>

## This is the main problem, we get a lot of weird rewritten URL's, where /front is rewritten into /cms3-unstable ##
<Domain www.def.com>
DocumentRoot /var/www/static_content
Alias /front /var/www/cms3-unstable
</Domain>

It rewrites /front into /cms3-unstable when a request is made.

I'll test some, more, but so far, PT doesn't see to make much difference (mainly because I think /cms3-unstable is not Aliasses, to passing it through the config again doesn't make any difference).
jasperroel
 
Posts: 9
Joined: Tue Mar 20, 2007 4:23 am

Postby richardk » Mon Jan 21, 2008 10:23 am

My config is as follows:

Our content management system: /var/www/cms3-unstable
(.htaccess: /var/www/cms/.htaccess)

Our httpd.conf [vhosts.conf] (simplified):

## This works fine without a RewriteBase ##
<Domain www.ghi.com>
DocumentRoot /var/www/cms3-unstable
# No Aliasses
</Domain>

If the .htaccess file is in /var/www/cms then it won't run for a request where the document root is not /var/www/cms and probably not for Aliases, either. It should probably be in /var/www/cms3-unstable.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jasperroel » Tue Jan 22, 2008 1:21 am

richardk wrote:If the .htaccess file is in /var/www/cms then it won't run for a request where the document root is not /var/www/cms and probably not for Aliases, either. It should probably be in /var/www/cms3-unstable.


You're absolutely right, but this is just a case of wrong copy-pasting. There is only one instance of the CMS, and the .htaccess file is in this directory.

We have multiple servers with this software, and every instance has a slightly different folder (/var/www, /srv, /var/websites, etc).

The .htaccess is in the same folder as the CMS, so that shouldn't be the problem. But thanks for noticing ;)!
jasperroel
 
Posts: 9
Joined: Tue Mar 20, 2007 4:23 am

Postby richardk » Wed Jan 23, 2008 11:02 am

Have you tried the %{ENV:BASE} mod_rewrite? What are your RewriteRules like?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jasperroel » Thu Jan 24, 2008 12:08 am

This is my full .htaccess:

Code: Select all
# Only do the rewriting if rewriting is enabled
<IfModule mod_rewrite.c>
   Options +FollowSymLinks

   RewriteEngine On

   # Only rewrite if the requested URI is not a file or directory
   RewriteCond %{REQUEST_FILENAME} -f [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^.* - [L]

   # Match everything before home/, eg. /front/ from /front/home/
   RewriteCond %{REQUEST_URI} ^(/.+/)home/.*$ [NC]

   # Make sure it starts with home. - means do nothing (carry on to the next rules)
   # E=BASE:%1 sets the %{ENV:BASE} variable to the bit before home/
   RewriteRule ^home(/.*)?$ - [NC,E=BASE:%1]

   # Now in the rules you can use %{ENV:BASE} in the rules to produce /abc/def/index.php?...
   RewriteRule ^home(.*)/? %{ENV:BASE}index.php?pages=$1 [QSA,NC,L]
   RewriteRule ^([0-9]+)/? %{ENV:BASE}index.php?id=$1 [QSA,NC,L]
   RewriteRule ^(.*bartcms)\.wsdl %{ENV:BASE}$1.php [QSA,NC,L]

</IfModule>


My specific problem here was that a customer wants al its static content readily available (eg www.example.org/document.doc) and the site through www.example.org/front/.

To do this, they made a VirtualHost like this

Code: Select all
<VirtualHost *:80>
   ServerName www.example.org
   DocumentRoot /srv/example.org/
   Alias /front /srv/cms3
   Alias /aduard /srv/cms3
   # And then some 80 or more Aliasses
</VirtualHost>


Now for some real life examples:
This is our own site (where it works):
http://www.appsoftware.nl/cms/software/bartcms.wsdl

And here our customer site (where is doesn't work, because of the Alias):
http://www.mijneigenbibliotheek.nl/adua ... rtcms.wsdl

Any hints or clues would be appreciated... Thanks for hearing me out :)!
jasperroel
 
Posts: 9
Joined: Tue Mar 20, 2007 4:23 am

Postby richardk » Thu Jan 24, 2008 12:26 pm

That's only going to set %{ENV:BASE} for home/. Does it work for home/ URLs?

Try replacing
Code: Select all
   # Match everything before home/, eg. /front/ from /front/home/
   RewriteCond %{REQUEST_URI} ^(/.+/)home/.*$ [NC]

   # Make sure it starts with home. - means do nothing (carry on to the next rules)
   # E=BASE:%1 sets the %{ENV:BASE} variable to the bit before home/
   RewriteRule ^home(/.*)?$ - [NC,E=BASE:%1]

with
Code: Select all
   RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
   RewriteRule ^(.*)$ - [E=BASE:%1]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jasperroel » Fri Jan 25, 2008 12:15 am

OMG... :o

Thank you so much (for the second time), that did the trick!

This will fix a number a problems and open a couple of more doors for me :).

Issue resolved, much appreciated!
jasperroel
 
Posts: 9
Joined: Tue Mar 20, 2007 4:23 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 20 guests

cron