RewriteCond file-exists check fails for existing files

Actually, I've never seen yet an article that have a tutorial about RewriteCond to get a variable from the RewriteRule below it.. However, you can still try to add a RewriteBase directive on the top of your .htaccess file:

RewriteBase /www/

Or this, just give both a try:

RewriteBase /var/www/

Or you can try one of these sets of directives below but EXCLUDING any RewriteBase directive that listed above:

RewriteCond /www/img/$2/$3/$4/$1 -f
RewriteRule ^www/img/(([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{4})[a-z0-9]{28}\.\w+)$ /www/img/$2/$3/$4/$1 [L]

Or you can even give this a try:

RewriteCond /var/www/img/$2/$3/$4/$1 -f
RewriteRule ^var/www/img/(([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{4})[a-z0-9]{28}\.\w+)$ /var/www/img/$2/$3/$4/$1 [L]

I'm sorry for copying the format of your rewrite condition and rule, because you said that “it works on your local development system.” But this is the simply rule that I recommend to you if you just want to rewrite /$var1.$var2 into /images/0a80/8e34/edaa/$var1.$var2, you can even add the others statically, one by one:

RewriteRule ^([a-zA-Z0-9_-]+).([a-zA-Z]{3})$ /images/0a80/8e34/edaa/$var1.$var2

Or you can even try this if you want to rewrite /img/0a808e34edaaeeffd973e4138789a4957d6b6a26.jpg dynamically into /img/0a80/8e34/edaa/0a808e34edaaeeffd973e4138789a4957d6b6a26.jpg with condition if the dynamic URL where it remapping is a file:

RewriteCond /img/$2/$3/$4/$1\.$5 -f
RewriteRule ^img/(([a-z0-9]{4})([a-z0-9]{4})([a-z0-9]{4})[a-z0-9]{28})\.([a-z]{3})$ /img/$2/$3/$4/$1.$5

Remember that the characters on "0a80", "8e34" and "edaa" must be exact as 4 and the chars. in "eeffd973e4138789a4957d6b6a26" must be as 28 and in "jpg" must be as 3. I hope that this will work, but I doubt..


If it works on localhost, but not test.com, I would guess its a problem with your filesystem or it is related to more general site configuration that you may be taking for granted. Did you install both operating systems, and then install the same packages, using the same procedure on both machines? What did each of the default configurations look like that shipped with each Apache? What about packages that modified default configuration before you even got a look?

Maybe your apache versions are near similar, but if they were from different sources, default configuration may have been different and may have been subsequently modified differently. Verify your configuration starting from the top. What is different? Trying making them look exactly the same (not just your rewrites, but Virtual-Host or Server contexts.)

The context of your Rewrite directives would likely help (as I see this being an issue of scope). Are the rewrites in the VirtualHost, Document-Root, Directory context?

The biggest difference I see from your posts between success and failure is this:

Relative

RewriteCond img/$2/$3/$4/$1 -f

Absolute

RewriteCond /usr/local/var/myapp/images/$2/$3/$4/$1 -f

Apache is capable of finding the file, so it shouldn't be permissions or any such. Are you using any Alias directives (localhost vs test.com)? If not, maybe you should try to Alias this specifically:

/var/www/myapp/current -> /var/www/myapp/releases/20130418090750

Put at least one hard path in your root configuration that doesn't require a symlink. You can wrap up your rewrites in a Directory context...

<Directory /var/www/myapp/current/webroot>
    rewrite statements...
</Directory>

What happens if you maintain your broken configuration on test.com, but where you currently have symlinks, try copying actual directories (temporarily) starting at the root. Just verify there is not some loose Symlink setting floating. Get rid of as many symlinks as possible and put them back in 1 by 1.