Bug 43128

Summary: mod_vhost_alias and mod_rewrite not working together
Product: Apache httpd-2 Reporter: Marko Hrastovec <marko.hrastovec>
Component: mod_vhost_aliasAssignee: Apache HTTPD Bugs Mailing List <bugs>
Status: RESOLVED DUPLICATE    
Severity: normal    
Priority: P2    
Version: 2.2.3   
Target Milestone: ---   
Hardware: Other   
OS: other   

Description Marko Hrastovec 2007-08-14 23:58:22 UTC
I have a problem with using mod_rewrite and mod_vhost_alias working together.

mod_rewrite is using some code to get document root. Obviously it get proper 
root from mod_alias or mod_userdir but from mod_vhost_alias it does not. When 
you use mod_rewrite and mod_vhost_alias together mod_rewrite gets the document 
root as the global document root for the server which is not correct

This is the comment from mod_rewrite where it expects to get document root.
/*
             * Hack because of underpowered API: passing the current
             * rewritten filename through to other URL-to-filename handlers
             * just as it were the requested URL. This is to enable
             * post-processing by mod_alias, etc.  which always act on
             * r->uri! The difference here is: We do not try to
             * add the document root
             */

I think mod_vhost_alias should return the correct document root here.
Comment 1 Rich Bowen 2007-08-15 05:03:20 UTC
This is the same issue as mentioned in 26052.

*** This bug has been marked as a duplicate of 26052 ***
Comment 2 Joshua Slive 2007-08-15 07:44:51 UTC
I actually doubt this person is reporting the same thing as bug 26052. But he
hasn't showed us at all what he is trying to do with mod_rewrite and
mod_vhost_alias and exactly what isn't working. Perhaps he isn't using the PT
flag for mod_rewrite.

But in any case:
1. Using mod_vhost_alias and mod_rewrite together is not generally recommended.
mod_vhost_alias is essentially just a simplified, restricted version of
mod_rewrite. If you need mod_rewrite anyway, you should just do all your
rewriting there.
2. You should address your question to a user-support forum like the
users@httpd.apache.org mailing list, being sure to provide details on your
configuration and exactly what isn't working.
Comment 3 Marko Hrastovec 2007-08-15 13:22:48 UTC
I can not agree that I shouldn't use mod_vhost_alias and mod_rewrite together.

I will try to explain why I would like both features in the same VirtualHost 
and why I think they should be used together.

We are using rewrite rules on PHP sites to simulate user friendly urls. We 
have many developers who are programming the same site maintained by 
subversion. Every programmer checks out the site to her/his home directory. If 
we put "*.hostname IN A x.x.x.x" in the DNS server, every programmer could 
have accessed his copy of the site via http://<username>.hostname.local/ just 
by following the rule to check out the site to proper directoy. 
VirtualDocumentRoot would enable every programmer to see his own copy. Rewrite 
rules would enable every programmer to simulate exactly the same behavior for 
user friendly URLs as on the main site.

Since mod_vhost_alias and mod_rewrite don't work together I have to invent 
some additional rules to simulate VirtualDocumentRoot along with rewrite rules 
which are already present. That way I have to have some very special rewrite 
rules applicable only for development copies and have to take special care 
that rules on the production server will work to. If I could use directives 
from both modules it would be much easier. That does not provide the 
functionality to have the same configuration on development and production 
servers. This is always potential problem for compatibility.

Here is the example of the VirtualHost I was trying to setup. It is IP based 
virtual hosting:
<VirtualHost x.x.x.x>
        VirtualDocumentRoot /home/%1/phpprojects/hostname/
        ServerName hostname
        CookieTracking on

        AddType application/x-httpd-php .php
        CustomLog logs/hostname.local.hal.si-combined_log combined

        RewriteEngine on
        RewriteRule  ^/attachment/ /attachment.php [NC,L]

        RewriteCond %{REQUEST_URI} ^(?!/images/).*               [NC]
        RewriteCond %{REQUEST_URI} ^(?!/img/).*                  [NC]
        RewriteCond %{REQUEST_URI} ^(?!/script/).*               [NC]
        RewriteCond %{REQUEST_URI} ^(?!/rss/).*                  [NC]
        RewriteCond %{QUERY_STRING} (.*)                         [NC]

        RewriteRule ^/(.*?)(/(\d+))?$ /index.php?ID=$3&url=$1&%1 [NC,L]
<VirtualHost>

I also don't think that the bug is the same as bug 26052. The bug 26052 says 
that DOCUMENT_ROOT environment variable was not set. In the case I am talking 
about not just the environment variable but also the document root itself is 
not set properly.

Possible workarounds I see now are to make a VirtualHost for every developer 
on the site with fixed DocumentRoot and rewrite rules as they will be on the 
production site. The second option is to make additional rewrite rules which 
are not going to be applied on the production server. The third option is my 
favorite and that is to use mod_vhost_alias and mod_rewrite together.

I will not change the status to "Reopen bug" because I am not the expert on 
Apache source code and design. I am just the user. After you consider my 
arguments do what ever you are pleased. I know I would be very pleased if I 
could use both modules together and in my case I can't see any reason why they 
shouldn't be. If you discard this bug I will probably take a look at the 
source and change it myself. It will take me much longer as would someone who 
is familiar with the code.
Comment 4 Marko Hrastovec 2007-08-15 14:11:37 UTC
I am very sorry I have bothered you with my problems. I thought I knew 
mod_rewrite pretty well. Well it has obviously been a while since I was 
familiar enough with this module. PT options on RewriteRule seems to be the 
right solution.

I am sorry again.