Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm a mod_rewrite novice and a regex noob, but here's what I want to do:

I've got a script that will re-size images on the fly. I'd like to be able to take any image file path, add a query string to it, and have it automatically sent to the script to process.

So something like:

from: http://mysite.com/images/logo.jpg?w=100&h=50 
to:   http://mysite.com/images/thumbs/phpThumb.php?src=logo.jpg&w=100&h=50

Of course, if no query string is specified, it should just point to the original file.

Using the .htaccess file in the /images, I've got this. But it doesn't work at all. Or at best, it break all my sites images:

 <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} ^.*
    RewriteRule ([^\s]+(\.(?i)(jpg|png|gif|bmp))$) /thumbs/phpThumb.php?src=$1 [L,QSA]
 </IfModule>
share|improve this question
    
This would probably get a higher response on stackoverflow –  George Jul 23 '10 at 21:04
1  
Actually did a quick search, and found more mod_rewrite discussion here. Besides, I got the answer I needed! –  Bryan M. Jul 24 '10 at 15:11

1 Answer 1

up vote 1 down vote accepted

First add a RewriteBase, this will strip the /images/ part in the query string. You will get photo.jpg?w=... instead of /images/photo.jpg?w=....

Second add /images/ to the redirection, because your current redirection tells to go to http://mysite.com/thumbs/phpThumbs... instead of http://mysite.com/images/thumbs/phpThumbs....

This gives something like this:

 <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /images/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{QUERY_STRING} ^.+
    RewriteRule ([^\s]+(\.(?i)(jpg|png|gif|bmp))$) /images/thumbs/phpThumb.php?src=$1 [L,QSA]
 </IfModule>
share|improve this answer
1  
I think you might want to change one line to: RewriteCond %{QUERY_STRING} ^.+ so it will match only if a query string is present –  Swish Jul 23 '10 at 19:59
    
Nice catch ;) thanks –  Weboide Jul 23 '10 at 20:10
    
Excellent! Works great (good to know I was close). I actually used RewriteCond %{QUERY_STRING} ^[wh] to limit the keys accepted, so as not to interfere with any other potential query params down the road. –  Bryan M. Jul 23 '10 at 21:05

Your Answer

 
discard

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.