Use a RewriteCond in virtual host using VirtualDocumentRoot

Using a single web hosting account to host multiple sites

Use a RewriteCond in virtual host using VirtualDocumentRoot

Postby presedo » Fri Nov 16, 2007 6:35 am

Hello

I have this config for a particular virtual host

Code: Select all
NameVirtualHost xxx.subdomain.domain.com:80
<VirtualHost xxx.subdomain.domain.com:80>
   ServerName  xxx.subdomain.domain.com
   ServerAlias *.subdomain.domain.com
   UseCanonicalName Off
   DocumentRoot "/path/to/files"
   VirtualDocumentRoot /path/to/files/%1
   <Directory "/path/to/files/xxx">
      Options +FollowSymLinks
      Options Includes FollowSymLinks 
      AllowOverride All
      Order allow,deny
        Allow from all
   </Directory>   
</VirtualHost>


This makes possible to use virtual hosts created on the fly
xxx.subdomain.domain.com -> /path/to/files/xxx
yyy.subdomain.domain.com -> /path/to/files/yyy
zzz.subdomain.domain.com -> /path/to/files/zzz

And works fine.

Now, I want to create a rewrite rule, but only if the file requested doesn't exists physically on the server.
Normally, on a normal virtual host, I used this code (which worked)

Code: Select all
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule . /path/to/files/xxx/make_the_thumbnail.php [L]


But once I put those lines in the previous code, the RewriteCond doesn't work, and the request is always redirected on make_the_thumbnail.php, even if the file requested (an image) exists.

I mean
xxx.subdomain.domain.com/folder/myfile.jpg -> /path/to/files/xxx/folder/myfile.jpg

myfile.jpg exists, but apache doesn't find it

Here is the code I use

Code: Select all
NameVirtualHost xxx.subdomain.domain.com:80
<VirtualHost xxx.subdomain.domain.com:80>
   ServerName  xxx.subdomain.domain.com
   ServerAlias *.subdomain.domain.com
   UseCanonicalName Off
   DocumentRoot "/path/to/files"
   VirtualDocumentRoot /path/to/files/%1
        RewriteEngine On
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteRule . /path/to/files/xxx/make_the_thumbnail.php [L]
   <Directory "/path/to/files/xxx">
      Options +FollowSymLinks
      Options Includes FollowSymLinks 
      AllowOverride All
      Order allow,deny
        Allow from all
   </Directory>   
</VirtualHost>


I suspect the RewriteCond line doesn't intercept the right filename, because of the DocumentRoot value that isn't equal to the VirtualDocumentRoot.

But how to correct it ?
presedo
 
Posts: 19
Joined: Fri Oct 06, 2006 6:10 am

Postby richardk » Fri Nov 16, 2007 1:13 pm

Try
Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.subdomain\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI} !-f
RewriteRule . %{DOCUMENT_ROOT}/%2/make_the_thumbnail.php [L]

instead of
Code: Select all
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /path/to/files/xxx/make_the_thumbnail.php [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby presedo » Tue Nov 20, 2007 11:02 am

You're amazing !!

Works indeed.

Thansk again !
presedo
 
Posts: 19
Joined: Fri Oct 06, 2006 6:10 am


Return to Domain Handling

Who is online

Users browsing this forum: No registered users and 2 guests

cron