views:

75

answers:

4

I'd like to use mod_rewrite to handle my requests of avatar images. I've got a folder containing all .png images and the file is named after the account's username.

So I would like requests for /avatar/Juddling, to show the image, /images/avatars/Juddling.png BUT if that file doesn't exist, I would like it to show a default.png image.

Options +FollowSymLinks
Options +Indexes
Options -MultiViews
RewriteEngine On

RewriteRule ^avatar/(.*)$ images/avatars/$1.png [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^avatar/(.*)$ images/avatars/default.png [NC,L]

Apache only seems to be handling the first RewriteRule, as I'm getting a 404 if the avatar isn't there rather than seeing the default image.

+1  A: 

Try it the other way round:

RewriteCond %{DOCUMENT_ROOT}images/avatars/$1.png -f
RewriteRule ^avatar/([^/]+)$ images/avatars/$1.png [L]
RewriteRule ^avatar/([^/]+)$ images/avatars/default.png [L]
Gumbo
this didn't work for me, i think it's because in the first condition, you're using the backlink $1, but apache hasn't been told which part of the URL to use yet, so is it looking for the file, /images/avatars/avatar/Juddling.png?because all requests are coming up as the default image.
Juddling
`$1` will hold the match of the corresponding `RewriteRule` pattern (see http://httpd.apache.org/docs/2.2/rewrite/rewrite_tech.html for the technical details). But you might need to add a `/` to the first expression: `%{DOCUMENT_ROOT}/images/avatars/$1.png`. If that’s not working, use mod_rewrite’s logging feature (see http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriteloglevel) to see what value that expression is evaluated to.
Gumbo
A: 

%{REQUEST_FILENAME} doesn't get updated when you rewrite, remaining the original value, so you need to get a little fancy:

RewriteRule ^avatar/(.*)$ images/avatars/$1.png [NC]
RewriteCond %{DOCUMENT_ROOT}/images/avatars/$1.png !-f
RewriteRule ^avatar/(.*)$ images/avatars/default.png [NC,L]
chaos
this is giving the same result as my .htaccess, showing a 404 rather than the default.png.
Juddling
+1  A: 

Try re-writting it so the test comes first such that:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^avatar/(.*)$ images/avatars/default.png [NC,L]

RewriteRule ^avatar/(.*)$ images/avatars/$1.png [NC,L]
grantc
A: 
Options +FollowSymLinks
Options -MultiViews

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/images/avatars/$1.png -f
RewriteRule ^avatar/(.*[^/])?$ /images/avatars/$1.png [L]
RewriteRule ^avatar/(.*[^/])?$ /images/avatars/default.png [L]

This code works! I've never really had a clue about regex but it's was the "(.*[^/])?" part that was causing the problem, I'm not sure what it mean but problem solved!

Thanks for your help guys!

Juddling
Why do you use `(.*[^/])?` anyways? Why would you allow the part after `/avatar/` to be empty?
Gumbo
I don't know what the expression means I'm afraid, I copied it from another example. But with just (.*) it always returns the default.png image, which means there is a problem with the condition.
Juddling