Forum Moderators: phranque

Message Too Old, No Replies

Hotlink Alt Image Not Working

Just get little red x

         

Duskrider

8:17 am on May 13, 2007 (gmt 0)

10+ Year Member



Topic pretty much says it. I've got hotlink protection working through .htaccess like a charm, it just isn't serving the alternate image I'd like it to serve when a request comes in from off site. Here's what my code looks like:

RewriteEngine on
#ReWrites for Hotlink Protection
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!^http://example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://example.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.example.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.example.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://example2.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://example2.com$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.example2.com/.*$ [NC]
RewriteCond %{HTTP_REFERER}!^http://www.example2.com$ [NC]
RewriteRule .*\.(jpg¦jpeg¦gif¦png¦bmp)$ http://www.example.com/images/promo/88x31button02.jpg [R,NC]
#End Hotlink Protection

I'm not sure if the code is wrong somewhere or if there's an option I don't have turned on or what. I'm on a shared box so I don't have access to much other than .htaccess as far as configuration goes. Most of the code above was generated by cPanel, though I've tried a bunch of different examples from the net too and none of them worked.

Thanks for any insight.

jdMorgan

2:15 pm on May 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good old cPanel -- Worst RewriteRule coder on the planet. Actually, it's bad mostly because the auto-generator does no optimizing. Try this simple, complete replacement for what you posted:

RewriteEngine on
#Redirect for Hotlink Protection
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example2\.com [NC]
RewriteCond %{REQUEST_URI} !^/images/promo/88x31button02\.jpg$
RewriteRule \.(jpe?g¦gif¦png¦bmp)$ http://www.example.com/images/promo/88x31button02.jpg [NC,R=302,L]
#End Hotlink Protection

It includes all of the functionality of your original, in a much more compact form. It also adds a provision to keep the code from looping, since the original code would have blocked access to the replacement image as well, leading to an 'infinite' redirection loop.

If you still have trouble, please post and explain what you see when 'it doesn't work'. You might consider installing the "Live HTTP Headers" extension to Firefox, so that you can watch the browser requests and server responses as the redirection takes place (or doesn't, as the case may be)... A very good basic tool for debugging.

Did you find that the "Options +FollowSymLinks" directive wasn't needed or caused a server error? If not, it should be included, just before the "RewriteEngine on" directive.

Replace all broken pipe "¦" characters in the code above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

Duskrider

4:42 pm on May 13, 2007 (gmt 0)

10+ Year Member



Awesome Jim, thank you so much. Worked perfectly right out of the box.

I was wondering if it had something to do with blocking the very image I was trying to show in the redirect. In the course of trying to get it to work cPanel deleted all my other redirects (non-www to www, index.php to / , etc), so I just used it to create the conditions for me out of frustration.

Needless to say I've created a backup copy of the .htaccess file and will never use cPanel to modify it again.

Thanks again.