Rewrite not working with symlinks

apache-2.2mod-rewrite

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
    </Directory>

Best Answer

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
Related Topic