Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am setting up a development server so I no longer have to directly edit the files on remote servers and to have SVN for the version management.

As some sites are very image heavy (3-30gb) we decided to not include the images in the SVN repositories.

So I set up a system which gives all subdomains their own virtual document root, so http://[user].[domain] gets a virtual document root of /working/[user]/[domain]/httpdocs these are the working copies of all the users.
(I don't know if it's relevant, but could be usefull)

However, for full testing purposes, it would be useful to display images. The dev server also doubles as backup server, so all the images are on the server. And I would like to display all missing images through a nifty htaccess/php script combination.

I am currently using these rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule (.*)$1 [L]

But this doesn't work exactly as planned, as I cannot get the subdomain info from it. Also, because the redirect url contains a full domain it gets redirected entirely, which makes it an extra http request.

The ideal setup would be that all missing images are served from a single PHP script, located at and that the images are served from their original url (like but I cannot seem to get it to work.

Is this even possible? If so, any pointers to what I should do?

p.s. As a bonus, the rules provided above seem to trigger on every image request on a double subdomain ( but it works like it should on, why is that?

This solved my issues:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.nl [NC]
RewriteCond /working/%1/%2/httpdocs%{REQUEST_URI} !-F
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteRule (.*)$1&host=%{HTTP_REFERER}&oeps=%{HTTP_HOST} [P]
share|improve this question
up vote 2 down vote accepted

If you would like access to the subdomain (I'm assuming for use in the PHP file), you can try the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$
RewriteCond %{HTTP_HOST} ([^\.]*)\.([^\.]*)\.testdomain\.com$
RewriteRule (.*)$1&user=%1&subdomain=%2 [P]

I changed the [L] flag to a [P], which will proxy the request, as opposed to redirecting it. This does require mod_proxy to be enabled. The %1 and %2 allow access to the regex groups from the previous RewriteCond

Also, I'm not sure as to why your RewriteRule is triggered on every request with two subdomains, as it shouldn't be. You can turn on the rewrite log, and see what it is matching against.

Hope this helps.

share|improve this answer
Your comment pushed me in the right direction, I have updated my post with the actual solution I used. Thanks! – gnur Apr 12 '11 at 13:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.