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].testdomain.com 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 (.*) http://testdomain.com/fallback.php?image=$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 http://testdomain.com/fallback.php and that the images are served from their original url (like http://me.webshop.testdomain.com/images/header.png) 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 (user.webshop.testdomain.com) but it works like it should on webshop.testdomain.com, 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 (.*) http://domain.nl/fallbackimages/fallback.php?image=$1&host=%{HTTP_REFERER}&oeps=%{HTTP_HOST} [P]

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 (.*) http://testdomain.com/fallback.php?image=$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.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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