Manipulating HTTP_HOST before apache selects virtual host

Using a single web hosting account to host multiple sites

Manipulating HTTP_HOST before apache selects virtual host

Postby nibblonian » Fri Oct 17, 2008 10:43 am

Hello,

what I want to do is to influence which virtual host handles a request. The only possibility is to adjust the HTTP_HOST variable realy early.
Can this be done using mod_rewrite or are there other possibilities (with or without mod_rewrite).

My goal is, that certain subdomains get handled by one virtualhost, even if this host does not normaly handle the domain. so for example webmail and phpmyadmin are handled by one virtualhost for all domains on the server.

phpmyadmin.domain1.com -> phpmyadmin.common.com
phpmyadmin.domain2.com -> phpmyadmin.common.com

a redirect using the htpp:// target and [R,L] works fine if inside !every! virtualhost, but is ignored if it is in the global config. And of course the URL visible in the browser is changed, which is somewhat not nice.


PS: It comes to my mind, that i perhaps can generate a virtualhost with
ServerName phpmyadmin.*
I'll check that. But I'm still interested in other possibilities, especially manipulating HTTP_HOST.

Thanks in advance
nibblonian
 
Posts: 5
Joined: Fri Oct 17, 2008 10:29 am

Postby richardk » Fri Oct 17, 2008 2:17 pm

PS: It comes to my mind, that i perhaps can generate a virtualhost with
ServerName phpmyadmin.*

That should work, just make sure it's before the other <VirtualHost>s that might match the same hostname.

a redirect using the htpp:// target and [R,L] works fine if inside !every! virtualhost, but is ignored if it is in the global config

I think that is normal behaviour. You might be able to get it to run if you put
Code: Select all
RewriteOptions Inherit

in the <VirtualHost>s, but i haven't tried.

And of course the URL visible in the browser is changed, which is somewhat not nice.

If you have mod_proxy you can use the P flag to proxy the requests.

I'm still interested in other possibilities, especially manipulating HTTP_HOST.

mod_setenvif might be able to change the HTTP_HOST variable.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nibblonian » Fri Oct 17, 2008 2:42 pm

Thanks for your reply. And yes, it works just fine.

If anybody is interested, here is my vhost config. the rewrite rules automatically choose the right folder for the subdomains independent of the domain. Of course this has the limitation, that sub-subdomains are not handled perfectly. Only the portion up to the first dot determines the folder used, but this is not a big problem for me.

Code: Select all
# php is handled by a fcgi wrapper
AddHandler fcgid-script .php

# virtual host for http protocol on all interfaces
<VirtualHost *:80>
        # this host handles the main domain and all common subdomains
        ServerName common-domain.com
        ServerAlias *.common-domain.com
        ServerAlias websql.*
        ServerAlias webmail.*
        ServerAlias webftp.*

        # user and group for executing cgi scripts (e.g. php)
        SuexecUserGroup www-common www-common

        # automatically redirect subdomains to their folders
        RewriteEngine On
                # prepend the FQDN to the URI
        RewriteRule ^(.+) %{HTTP_HOST}$1
                # remove leading www. (if present)
        RewriteRule ^www\.(.+) $1
                # extract the subdomain to access the right folder
        RewriteRule ^([^./]+)\.[^/]+/(.*) /srv/webserver/common/data/$1/$2

        # define and configure the document root
        DocumentRoot "/srv/webserver/common/data/"
        <Directory "/srv/webserver/common/data/">
                FCGIWrapper /var/www/common/php-fcgi-starter .php
                Options ExecCGI FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        # configure logging
        RewriteLog "/srv/webserver/common/log/apache-rewrite.log"
        RewriteLogLevel 1
        ErrorLog "/srv/webserver/common/log/apache-error.log"
        LogLevel warn
        CustomLog "/srv/webserver/common/log/apache-access.log" combined

        # try not to identify as a possibly vulnerable version
        ServerSignature Off
</VirtualHost>
nibblonian
 
Posts: 5
Joined: Fri Oct 17, 2008 10:29 am

Postby richardk » Sat Oct 18, 2008 4:06 am

Of course this has the limitation, that sub-subdomains are not handled perfectly. Only the portion up to the first dot determines the folder used, but this is not a big problem for me.

By the mod_rewrite part? How would you like them handled?

You might also want to look at VirtualDocumentRoot.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nibblonian » Sat Oct 18, 2008 4:20 am

Yes, the mod_rewrite causes this limitation. But I don't think it is possible to find any expression to handle this without explicitly naming the domains.
This is because some domains I use are directly beneath the TLD and others are hostnames in some larger network. Also there exist TLDs with a dot e.g. .co.uk (is this whole think then called TLD or just the uk).

Because of this I can't decide only by looking at the dots where in the FQDN the subdomain, hostname and TLD is.
I want my expression to be independent of hostname and TLD to reduce the administrative work when adding new domains and therefore only take into account the string up to the first dot to select the correct folder.


If you don't agree with me on the statement that I can't achieve both, domain independency and sub-subdomain redirecting, I'm very excited about your solution. This would be really cool.
But I think this is impossible by design.
nibblonian
 
Posts: 5
Joined: Fri Oct 17, 2008 10:29 am

Postby nibblonian » Sat Oct 18, 2008 4:26 am

I just looked at virtualdocumentroot you pointed me at and then it came to my mind that in fact it can be done if I don't limit foldernames to the name of the subdomain but give it the name of the FQDN or do a folder structure where dots are replaced with / and then the whole thing is flipped like /somepath/TLD/hostname/subdomain/subsubdomain
nibblonian
 
Posts: 5
Joined: Fri Oct 17, 2008 10:29 am

Postby richardk » Sat Oct 18, 2008 9:25 am

Please be more specific about what structure you want, as it really depends on what directory names you are prepared to have.

You could just have ./sub.example.co.uk/ and /example.com/
Code: Select all
VirtualDocumentRoot /srv/webserver/common/data/%0/

or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC,OR]
RewriteCond %{HTTP_HOST}      ^(.+)$
RewriteRule ^(/.*)$ /srv/webserver/common/data/%1/$1 [PT,L]


You could have ./uk/sub.example.co.uk/ and /com/example.com/
Code: Select all
VirtualDocumentRoot /srv/webserver/common/data/%-1/%0/

or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+\.([^\.]+))$ [NC,OR]
RewriteCond %{HTTP_HOST}      ^(.+\.([^\.]+))$
RewriteRule ^(/.*)$ /srv/webserver/common/data/%2/%1/$1 [PT,L]


You could have ./uk/sub.example.co/ and /com/example/
Code: Select all
VirtualDocumentRoot /srv/webserver/common/data/%-1/-2+/

or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)\.([^\.]+)$ [NC.,OR]
RewriteCond %{HTTP_HOST}      ^(.+\.([^\.]+))$
RewriteRule ^(/.*)$ /srv/webserver/common/data/%2/%1/$1 [PT,L]


You could have ./uk/co/sub.example/ and /com/example/_/
Code: Select all
VirtualDocumentRoot /srv/webserver/common/data/%-1/-2/-3+/

or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.((.+)\.)?([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST}      ^((.+)\.)?([^\.]+)\.([^\.]+)$
RewriteCond %2_.%3.%4 ^((.+)_|(_))\.([^\.]+)\.([^\.]+)$
RewriteRule ^(/.*)$ /srv/webserver/common/data/%5/%4/%2%3/$1 [PT,L]


You could have ./uk/co/example/sub/ and /com/example/_/_/
Code: Select all
VirtualDocumentRoot /srv/webserver/common/data/%-1/-2/-3/-4+/

or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(((.+)\.)?([^\.]+)\.)?([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST}      ^(((.+)\.)?([^\.]+)\.)?([^\.]+)\.([^\.]+)$
RewriteCond %3_.%4_.%5.%6 ^((.+)_|(_))\.(([^\.]+)_|(_))\.([^\.]+)\.([^\.]+)$
RewriteRule ^(/.*)$ /srv/webserver/common/data/%8/%7/%5%6/%2%3/$1 [PT,L]


The last two mod_rewrites are untested and could be a bit prettier.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby nibblonian » Mon Nov 03, 2008 3:36 am

Thanks for your reply. Some nice schemes.
nibblonian
 
Posts: 5
Joined: Fri Oct 17, 2008 10:29 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 17 guests

cron