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

I'm trying to match URLs of this type:

I'd then like to check whether this file exists:

If it does, I'd like to rewrite the URL to point to that file.

If it doesn't I'd like to rewrite the URL to point to my image-generating script:

Here's what I've got so far:

RewriteCond %{REQUEST_FILENAME} ^/image/([0-9]+)$
RewriteCond %{DOCUMENT_ROOT}/files/images_%1.jpg !-f
RewriteRule ^image/([0-9]+)$ img_gen.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^image/([0-9]+)$ /files/images_$1.jpg [L]

But all I'm getting is a 404.

share|improve this question
Have you tried creating and reviewing an Apache rewrite log? - may help with diagnosis and you can paste the output in your question. – EightBitTony May 14 '12 at 13:39
I wish I could, but I'm on a shared host. Maybe I'll try it locally on a MAMP environment. – jawns317 May 14 '12 at 13:48
You can add the rewrite log commands to your .htaccess file - see the first answer so far – EightBitTony May 14 '12 at 13:50

If you can't figure out what your Rewrite Rules are doing you should enable RewriteLog with some increased verbosity:

RewriteLogLevel 3
RewriteLog "/var/log/rewrite.log"

This should give you a pretty good starting position in further investigation of what's going on wrong.

share|improve this answer

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.