Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirecting images

RewriteRule not working?

         

IanWatson

2:13 pm on Sep 22, 2007 (gmt 0)

10+ Year Member



Hey, everyone. Got some problems with my .htaccess file.

Now, I've currently got some existing RewriteRules going on which are successfully working (preventing hotlinking), so I know mod_rewrite is active.

For background: I've got a php file set up at the moment. If no image string is passed to it, it grabs a random image from whichever directory it's in. Height and width variables can also be passed, which will dynamically resize the image in question.

So if I use reboth.php?h=50&w=75, I'll get a 50x75 image.

Right now I'm trying to set things up so I can request, for example, 50x75.png, and it'll automatically redirect to the above URL.

This is what I've got:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://([^.]+\.)?example.org/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://([^.]+\.)?otherexample.com/.*$ [NC]
RewriteRule ^([0-9]+)x([0-9]+)\.png$ reboth.php?w=$1&h=$2 [NC]

I've tried variables of this... using \.png.*, giving the full URL for the reboth.php file... I've even tried "hard-coding":


RewriteRule ^.*50x50\.png$ http://sub.example.org/gallery/reboth.php?w=50&h=50

No luck so far. I keep getting my error page. I can't see what I might be doing wrong. Any suggestions?

[edited by: jdMorgan at 6:36 pm (utc) on Sep. 22, 2007]
[edit reason] example.com [/edit]

jdMorgan

4:13 pm on Sep 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your RewriteConds specifically disable this rule if the request referrer is "domain.org," "otherdomain.org," or blank. In other words, if "domain.org" is your own domain, then the rule won't apply to any requests referred from your own domain. Are you sure that's what you intended?

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

IanWatson

5:43 pm on Sep 22, 2007 (gmt 0)

10+ Year Member



... Wow, I can't believe what an idiot I am. I copied and pasted that section from my hotlink-disabling upper-level .htaccess file without even thinking about it.

Dur.

Thanks.

g1smd

1:24 am on Sep 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You specifically asked for a redirect in your question, but your example code is for a rewrite.

A rewrite is actually what you require, but you also need to block access to the direct dynamic URLs too.

The easist way is to 301 redirect those to the URL that you do want to use. That redirect will not interfere with your internal rewrite if you are careful.