0

I'm trying to match URLs of this type:

http://www.example.com/image/12345

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

http://www.example.com/files/image_12345.jpg

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:

http://www.example.com/img_gen.php?id=12345

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.

jawns317
  • 161
  • 1
  • 1
  • 4
  • Have you tried creating and reviewing an Apache rewrite log? http://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

1 Answers1

0

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.

milosgajdos
  • 1,808
  • 2
  • 21
  • 29