Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hotlinking and .htaccess

Status
Not open for further replies.

Glowball

Programmer
Oct 6, 2001
373
0
0
US
Hi all,

I'm new to .htaccess and I'm trying to redirect all image hotlinking to a page on my site. I also need to pass the requested image to the page as a parameter. I can't seem to get it to work, can someone point me in the right direction?

[tt]
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^ [NC]
RewriteRule .*\.(jpg|jpeg)$ [R,NC]
[/tt]

So if someone hotlinks to [tt][/tt] I want them to pull down [tt][/tt] instead (this PHP file produces an image in the JPEG MIME type).

What do you all think? I know I can use mod_rewrite, btw. Thanks!
 
Thanks -- I've gone through a lot of tutorials and the problem is basically getting the original image to the URL. I'm having problems understanding the "$1" part and how I specify what that is. I'm on a shared host -- how do I get at a rewrite log?

I've successfully been able to redirect to the script, but the output is the default image that shows when there is nothing in the parameter. In other words, it acts as though I've sent them to [tt][/tt] instead. It's not passing the original information.
 
Ahh, ok the $1 is a matching operator. It puts whatever you match into that variable

So you have
Code:
RewriteRule .*\.(jpg|jpeg)$ [URL unfurl="true"]http://www.mydomain.com/hotlinks.php?j=$1[/URL] [R,NC]
In this cae nothing is matched so $1 is empty.

If you do this
Code:
RewriteRule (.*\.(jpg|jpeg))$ [URL unfurl="true"]http://www.mydomain.com/hotlinks.php?j=$1[/URL] [R,NC]
It will put whatever it matched into $1

Hopefully :) Its a regular expression thing. I am not testing it here but thats the general idea, the () tell the regex to 'save what I match' and it places the values in $1
 
OHHH! Okay that makes sense! Let me try that...
 
Okay this seems to be close:

Code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^[URL unfurl="true"]http://(www\.)*mydomain.com(/).*$[/URL] [NC]
RewriteRule (.*\.(jpg|jpeg))$ [URL unfurl="true"]http://www.mydomain.com/hotlinking.php?j=$1[/URL] [R,NC]

I needed an asterisk after the It's still not getting a "true" when it hits the RewriteRule and I'm confused about why. What does the second line mean? Isn't it saying "if the http_referer isn't an empty string?" If I comment that line out everything just hangs.

Now if http_referer was and it was calling a JPEG on mydomain.com, would it make it to the RewriteRule?
 
Correct, it is saying 'if the referrer is NOT blank AND the referrer is NOT my server the rewrite rule applies.'

I'd remove the RewriteCond %{HTTP_REFERER} !^$ and see what happens. Seems odd since that will never evaluate properly.
 
If I take that line out and go to mydomain.com all the GIFs load but the JPEGs just hang when trying to load. What does that mean?

Also, would this line allow all requests for any graphic named "mypic.jpg" to pass through and not get handled by the rewrite?

Code:
RewriteCond %(REQUEST_URI)  !.*mypic.jpg$ [NC]

Thanks!
 
I got it to work! I decided to go back to square one and just do it as simply as possible and the easy way worked. This is how I did it:
Code:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !(.*)mydomain.com(.*)$ [NC]
RewriteCond %(REQUEST_URI)  !(.*)mypic.jpg$ [NC]
RewriteRule (.*\.(jpg|jpeg))$ [URL unfurl="true"]http://www.mydomain.com/hotlinking.php?j=$1[/URL] [R,NC]
Thanks so much for everyone's help!
[thumbsup]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top