Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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

We want to serve up an image as apache is so good at doing, and when the client requests a thumbnail, the request will look like the following: [http://host/photos/im/image_w200.jpg] (w200 is the requested width of the thumbnail).

Sometimes the thumbnails will not exist. When they don't exist we want apache to forward the request to our application. That request will look like [http://host/create_thumbnail?file=image_w250.jpg]. The app will create the thumbnail, and redirect back to the original request.

This worked fine when I had normal files and directories in /home/dev/x/y/mediadir. However /home/dev/x/y/mediadir/photos is now a symbolic link to the /home/dev/photos directory. So now, even when the file exists, it is always passing the !-f RewriteCond and rewriting to thumbnail the photo.

Things I have tried:

  • the server has access to the symlink dirs. When I take out the rewrite block, it works fine
  • I have tried !-l, !-F, and !-U. !-U serves the file when it exists, but when it doesn't exist apache doesn't rewrite the request. !-l, and !-F do the same thing as !-f

Anyone have any suggestions?

<VirtualHost *:443>

        #... cut ...

    RewriteLog /home/dev/logs/rewrite.log
    RewriteLogLevel 3

    Alias /media/ /home/dev/x/y/mediadir/
    <Directory /home/dev/x/y/mediadir>
        Order allow,deny
        Allow from all
        Options FollowSymLinks

        RewriteEngine on
        RewriteCond photos/../$ !-f
        RewriteRule ^photos/../(.+)$ https://%{HTTP_HOST}/create_thumbnail?file=$1
share|improve this question
up vote 2 down vote accepted

Instead of using one RequestCond (RewriteCond photos/../$ !-f), I used two. The first to check if the REQUEST_FILENAME matches the regex, and the second to check that the file doesn't exist. This is working well for me.

    RewriteCond %{REQUEST_FILENAME} photos/../.+
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^photos/../(.+)$ https://%{HTTP_HOST}/create_thumbnail?file=$1
share|improve this answer
Thanks a ton sir. – Alexandre Bourlier Jul 9 '14 at 11:09

I'm kind of confused what your paths are, because you refer to /mediadir, but there is no /mediadir (referenced from root; not in the file system, nor in apache). So I don't really know what you mean, but perhaps you can fix it by adding:

<Directory /path/to/photos/>
    Order allow,deny
    Allow from all

What does your rewrite log say?

share|improve this answer
I see what you mean by the confusing nature of how I had my directories, so I fixed the directory names, and the references to them in my question. I will look into adding the <Directory directive. – Frank Henard May 5 '11 at 22:15

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.