You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Damian Meyer <ap...@damianmeyer.com> on 2004/01/30 10:58:33 UTC

[users@httpd] combining mod_rewrite and mod_vhost_alias

Hi,

I am trying to move from a scenario with many thousand static 
<VirtualHost> blocks in a config to one where we use mod_vhost_alias.  
So far, that module works well.

The scenario is to have the document root for each host in 
/home/sites/d/o/m/domain.tld/www for a host called domain.tld.  That 
part, and the VirtualDocumentRoot and VirtualScriptAlias commands as 
below work well.  For example, a request for http://aaa.com/index.html 
returns /home/sites/a/a/a/www/index.html as expected.

Now for the hard part, I also want www.domain.tld to return the same 
index.html file, without having a symbolic link at 
/home/sites/w/w/w/www.domain.tld/ which defeats the purpose of splitting 
up into three layers of directories.  Hence my question about 
mod_rewrite:  How can I strip the www. off the hostname before passing 
it to mod_vhost_alias?

I have tried this solution with both HTTP_HOST and SERVER_NAME as the 
environment variables being modified in the RewriteRule - does the 
RewriteRule permit modification of these system environment variables or 
just ones you set yourself?  What variable does VirtualDocumentRoot use 
to get it's data for it's %N.M substitutions?

  RewriteEngine On
  RewriteLog "/usr/local/apache/logs/rewrite.log"
  RewriteLogLevel 3

  RewriteMap  lowercase  int:tolower
  RewriteCond  ${lowercase:%{HTTP_HOST}}  ^www\.(.+)$
  RewriteRule ^/(.*)$ /$1 [PT,E=HTTP_HOST:%1]

  VirtualDocumentRoot /home/sites/%1.1/%1.2/%1.3/%0/www
  VirtualScriptAlias /home/sites/%1.1/%1.2/%1.3/%0/cgi-bin

I do have a solution involving a redirect:

  RewriteMap  lowercase  int:tolower
  RewriteCond  ${lowercase:%{HTTP_HOST}}  ^www\.(.+)$
  RewriteRule ^/(.*)$ http://%1/$1 [R]

But I don't want to go down that path as in some rare cases I don't 
always have control over the DNS and can't guarantee that domain.tld 
resolves to the same IP as www.domain.tld.

Alternatively, is there a way to conditionally have two different calls 
to VirtualDocumentRoot depending on www.domian.tld versus domain.tld?  
For example (in pseudo code):

if HTTP_HOST matches ^www\.(.+)$ then
  VirtualDocumentRoot /home/sites/%2.1/%2.2/%2.3/%2+/www
  VirtualScriptAlias /home/sites/%2.1/%2.2/%2.3/%2+/cgi-bin
else
  VirtualDocumentRoot /home/sites/%1.1/%1.2/%1.3/%0/www
  VirtualScriptAlias /home/sites/%1.1/%1.2/%1.3/%0/cgi-bin
endif

Any ideas, gentlefolk?  This is Apache 1.3.29 on linux.

Kind regards
Damian Meyer


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] combining mod_rewrite and mod_vhost_alias

Posted by Damian Meyer <ap...@damianmeyer.com>.

Joshua Slive wrote:

>On Thu, 5 Feb 2004, Damian Meyer wrote:
>  
>
>>However, when it comes to CGI scripts, there seems to be no way to use
>>suEXEC to wrap these to run as the actual user, not the nobody user.
>>I've looked at cgiwrap and mod_cgiwrap which seems to be part way
>>there.  However the DOCUMENT_ROOT is not correct.  So to my question: is
>>there any way to modify the DOCUMENT_ROOT variable within mod_rewrite in
>>httpd.conf?   I suspect the answer is no, but thought I would ask.
>>    
>>
>
>You suspect correctly.  The answer is no.
>
>The main reason is that DOCUMENT_ROOT is not a variable that should be
>used.  It does not take into account Aliases, RewriteRules, etc, and
>therefore is unreliable.
>
>But I don't see why you need to use DOCUMENT_ROOT anyway.  I'm not a
>cgiwrap expert, but it doesn't really seem to be required.
>  
>

cgiwrap works by either having the username to wrap the cgi passed as an 
argument on the extra path info, or by getting the user that owns the 
DOCUMENT_ROOT as the user to exec the cgi under.  Tied in with 
mod_cgiwrap that automates this and the second option, the user that 
owns the DOCUMENT_ROOT becomes important.  Unfortunately this clashes 
with the mod_rewrite way of doing dynamic virtual hosts.

It's time I think to dive under the hood of these and hack something, 
despite my very rusty C programming skills I think.

Just as an aside, Apache really needs an industrial strength dynamic 
virtual host module that deals with multiple server aliases and with 
wrapping cgis as the user.  If mod_vhost_alias had some rewrite 
capabilities or conditional rule selection, and a way of tying in with 
suEXEC, then that would be ideal.  I suspect a lot of large ISPs have 
internal solutions to these problems, although I can't find anything 
released back to the community that does it.

Rgds
Damian


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] combining mod_rewrite and mod_vhost_alias

Posted by Joshua Slive <jo...@slive.ca>.

On Thu, 5 Feb 2004, Damian Meyer wrote:
>
> However, when it comes to CGI scripts, there seems to be no way to use
> suEXEC to wrap these to run as the actual user, not the nobody user.
> I've looked at cgiwrap and mod_cgiwrap which seems to be part way
> there.  However the DOCUMENT_ROOT is not correct.  So to my question: is
> there any way to modify the DOCUMENT_ROOT variable within mod_rewrite in
> httpd.conf?   I suspect the answer is no, but thought I would ask.

You suspect correctly.  The answer is no.

The main reason is that DOCUMENT_ROOT is not a variable that should be
used.  It does not take into account Aliases, RewriteRules, etc, and
therefore is unreliable.

But I don't see why you need to use DOCUMENT_ROOT anyway.  I'm not a
cgiwrap expert, but it doesn't really seem to be required.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] combining mod_rewrite and mod_vhost_alias

Posted by Damian Meyer <ap...@damianmeyer.com>.
Many thanks to all who replied to my email last week.  I have since
solved my problems using solely mod_rewrite and it works well for HTML
files or php scripts, etc.

However, when it comes to CGI scripts, there seems to be no way to use
suEXEC to wrap these to run as the actual user, not the nobody user.
I've looked at cgiwrap and mod_cgiwrap which seems to be part way
there.  However the DOCUMENT_ROOT is not correct.  So to my question: is
there any way to modify the DOCUMENT_ROOT variable within mod_rewrite in
httpd.conf?   I suspect the answer is no, but thought I would ask.

Alternatively, does anyone have another way to wrap cgis for dynamically
configured virtual hosts using mod_rewrite (or even mod_vhost_alias).?

Kind regards
Damian Meyer.


Joshua Slive wrote:

>On Fri, 30 Jan 2004, Damian Meyer wrote:
>  
>
>>Now for the hard part, I also want www.domain.tld to return the same
>>index.html file, without having a symbolic link at
>>/home/sites/w/w/w/www.domain.tld/ which defeats the purpose of splitting
>>up into three layers of directories.  Hence my question about
>>mod_rewrite:  How can I strip the www. off the hostname before passing
>>it to mod_vhost_alias?
>>    
>>
>
>
>2. Do the whole thing in mod_rewrite.  Everything that mod_vhost_alias can
>do, mod_rewrite can also do.  Plus mod_rewrite has a ton more flexibility,
>including the ability to do things conditional on the hostname starting
>with "www".  There are some examples in the virtual host and mod_rewrite
>docs.  Combining mod_rewrite and mod_vhost_alias is a very complicated
>thing to do, since you need to be intimately familiar with the internal
>workings of apache to know how they will interact.
>
>Joshua.
>
>  
>


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] combining mod_rewrite and mod_vhost_alias

Posted by Joshua Slive <jo...@slive.ca>.
On Fri, 30 Jan 2004, Damian Meyer wrote:
> Now for the hard part, I also want www.domain.tld to return the same
> index.html file, without having a symbolic link at
> /home/sites/w/w/w/www.domain.tld/ which defeats the purpose of splitting
> up into three layers of directories.  Hence my question about
> mod_rewrite:  How can I strip the www. off the hostname before passing
> it to mod_vhost_alias?

It may be possible to do what you want, but I'm not sure.

What I would recommend is one of the following:

1. Use the redirect solution.  I know you said you don't want this, but in
general it is a bad idea to have two different names mapping to exactly
the same content.  The redirect assures that there is only a singal
canonical name in search engines, caches, etc.

2. Do the whole thing in mod_rewrite.  Everything that mod_vhost_alias can
do, mod_rewrite can also do.  Plus mod_rewrite has a ton more flexibility,
including the ability to do things conditional on the hostname starting
with "www".  There are some examples in the virtual host and mod_rewrite
docs.  Combining mod_rewrite and mod_vhost_alias is a very complicated
thing to do, since you need to be intimately familiar with the internal
workings of apache to know how they will interact.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] combining mod_rewrite and mod_vhost_alias

Posted by Jonas Eckerman <jo...@frukt.org>.
On Fri, 30 Jan 2004 17:58:33 +0800, Damian Meyer wrote:

[Lots of info about problems when trying to combine mod_vhost and mod_rewerite...]

>  Any ideas, gentlefolk?  This is Apache 1.3.29 on linux.

Not sure what the best solution is, but you I believe you could use mod_perl to write your own vhost handler that does exactly what you want.

Regards
/Jonas

-- 
Jonas Eckerman, jonas_lists@frukt.org
http://www.fsdb.org/


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org