1

Dear folks, Trying to rewrite images with a rewrite rule that should only fire when two conditions exist: image starts with IMG or better yet IMG- and secondly the file should exist.

Nice url: IMG-folder/file.jpg fetched ugly url image under water.

The code below works when no conditions are set, as well as when only the second !-f condition is set, but not when both rules coexist. Why is this? Ideas/code/comments are very appreciated. Thanks

Q1. Im a bit confused: is the first condition actually necessary? Since I'm stating to only rewrite IMG-.... files to the ugly urls under water.

Q2. Why does this code below go wrong then both conditions are fired?

RewriteCond %{REQUEST_URI} ^IMG.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^IMG-(.+)_w(.+)_h(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3 [L]
Sam
  • 15,254
  • 25
  • 90
  • 145

1 Answers1

1

%{REQUEST_URI}, always contains the full requested path. The first argument of RewriteRule on the other hand is relative. If you match a %{REQUEST_URI} against a pattern that specifies the start position, ^, you should also include the leading slash.

RewriteCond %{REQUEST_URI} ^/IMG.*$

I'm also slightly confused about the form of the virtual URLs. Does IMG occur twice in a typical image URL? Is /IMG-folder/IMG-12_w34_h56.jpg a valid virtual filename?

nitro2k01
  • 7,627
  • 4
  • 25
  • 30
  • +1 thanks @nitro No: only at the beginning once marking the shorter url for me so only once at the beginning, after that never... – Sam Mar 21 '11 at 03:30
  • Thanks very much @nitro, this fixed the job!! (by the way I'm still trying to get rewrite LOG to work so I can proceed with out ultimate One Rule to Rule them All quest: http://stackoverflow.com/questions/5371848/stuck-with-ingenius-optional-url-rewriting-in-htaccess-with-apache-1-rule-to-rul I will report back soon when I have information on the log. Cheers for now, from Holland. – Sam Mar 30 '11 at 07:37
  • An artistically awesome Electron Voltage Music show you gave on your page @Nitro with that "Magnus Danielson Sawtooth phase *shifter*". Nice boys toys! – Sam Mar 30 '11 at 07:48