0

I am trying to make use of an image processing app. It requires an Apache directive in a .htaccess file. But no matter what I do to the RewriteRule, I cannot getting it to work. Causes a Server 500 error.

The rule is:

RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php

This should take any jpg/gif/png file and send it over to the adaptive-images.php file, where it will be run through a process for image sizing.

At this point I am beyond my expertise for RewriteRules.

Bill the Lizard
  • 352
  • 1
  • 7
  • 15

1 Answers1

1

The first parameter of the RewriteRule directive is a regex so you need to escape the period: \.. Your rule also won't match because you need to match against the whole URI and not just the file extension. Thus the .* is necessary to match whatever might come before the extension (e.g. filename.jpg or /images/filename.jpg).

RewriteRule .*\.(?:jpe?g|gif|png)$ adaptive-images.php

I don't believe forgetting this will cause a 500 error on its own so you should review your Apache error log for more details if this doesn't fix the issue.

Also, using this redirect won't "send" the image file anywhere unless you are doing some trickery in PHP to read the HTTP referer as otherwise the redirected page will have no knowledge of which image it was "passed."

Kkarsko
  • 15
  • 3
Garrett
  • 1,332
  • 10
  • 16
  • PHP will see the original request in `$_SERVER["REQUEST_URI"]`. – Michael Hampton Oct 11 '12 at 02:58
  • Either way my point was it has to be stripped from a superglobal which are highly server-dependent and not always present. It would be more effective to redirect with the image in the URI as a GET variable IMO. – Garrett Oct 11 '12 at 03:03
  • Now you're getting into issues of programming, which we don't really do here. :) – Michael Hampton Oct 11 '12 at 03:09
  • I merely meant it as a helpful aside since the OP clearly stated they're not much of a mod_rewrite guru. But alas, here we are. :) – Garrett Oct 11 '12 at 03:14
  • Thanks gman. I actually tried it with the beginning slash and still failed. I did go look at the error logs and I did not see any reference to the issue in the log, so I am at a loss. – erick_the_redd Oct 11 '12 at 04:57
  • I update my post as your regex still wouldn't match anything since you are matching against the entire URI (the part after the hostname in the URL) as a string. As for the 500, does calling your adaptive-images.php file directly work? Perhaps that script is causing the 500. – Garrett Oct 11 '12 at 05:09
  • Well gman, I tried your update and still throws a 500. I don't believe it is even getting as far as running the php script. I was told that you could test the RewriteRule by doing this: RewriteRule .*\.(?:jpe?g|gif|png)$ some-picture.jpg. If the RewriteRule is correct, then all images on the server should come back with 'some-picture.jpg'. So I can only assume there is something specific to the RewriteRule. From what I have seen it works great on Apache 2.x, but the is not an option for me at the moment. darn it. – erick_the_redd Oct 11 '12 at 05:22
  • Check the permissions on your .htaccess and make sure they're proper for your web user. I'm surprised the Apache error log is mum on this subject as it typically gives more detail about these errors. You may want to check with your server admin to make sure this logging is enabled. – Garrett Oct 11 '12 at 05:49