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.

  • Have you tried creating and reviewing an Apache rewrite log? httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritelog - 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.