homepage Welcome to WebmasterWorld Guest from 54.196.225.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
prevent hotlinking using .htaccess
prevent hotlinking using .htaccess
lcutting




msg:3066843
 3:54 am on Aug 31, 2006 (gmt 0)

I am attempting to prevent our content from being hotlinked to. We host a number of flash presentations that we've found people linking to.

I've done a large amount of reading and it appears that .htaccess should be able to do the job but have yet to find a code that works properly.

.htaccess for password protection works so I know that the Apache is set properly.

Can you help me with this?

Thanks,

 

compose




msg:3066864
 4:19 am on Aug 31, 2006 (gmt 0)

Hello,

may be it will work, try this
------------------------------

Example: Your site url is www.mysite.com. To stop hotlinking of your images from other sites and display a replacement image called nohotlink.jpe placed in your images directory, place this code in your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_REFERER}!^http://(.+\.)?mysite\.com/ [NC]
RewriteCond %{HTTP_REFERER}!^$
RewriteRule .*\.(jpe?g夙if在mp如ng)$ /images/nohotlink.jpe [L]

The first line of the above code begins the rewrite. The second line matches any requests from your own mysite.com url. The [NC] code means "No Case", meaning match the url regardless of being in upper or lower case letters. The third line means allow empty referrals. The last line matches any files ending with the extension jpeg, jpg, gif, bmp, or png. This is then replaced by the nohotlink.jpe file in your images directory. This JPEG image is using the extension jpe instead of jpg to prevent blocking your own replacement image.

To stop hotlinking from specific outside domains only, such as myspace.com, blogspot.com and livejournal.com, but allow any other web site to hotlink images:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blogspot\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://(.+\.)?livejournal\.com/ [NC]
RewriteRule .*\.(jpe?g夙if在mp如ng)$ /images/nohotlink.jpe [L]

You can add as many different domains as needed. Each RewriteCond line should end with the [NC,OR] code. NC means to ignore upper and lower case. OR means "Or Next", as in, match this domain or the next line that follows. The last domain listed omits the OR code since you want to stop matching domains after the last RewriteCond line.

You can display a 403 Forbidden error code instead of an image. Replace the last line of the previous examples with this line:

RewriteRule .*\.(jpe?g夙if在mp如ng)$ - [F]

Warning: Do not use .htaccess to redirect image hotlinks to another HTML page or server that isn't your own (such as this web page). Hotlinked images can only be replaced by other images, not with an HTML page.

As with any htaccess rewrites, you may block some legitimate traffic (such as users behind proxies or firewalls) using these techniques.

Warm Regards,
Vineet

lcutting




msg:3067714
 6:05 pm on Aug 31, 2006 (gmt 0)

That at least gets some action. But instead of getting what I was expecting, I get an Internal Server Error message.

When I delete the file, everything works.

At least this is one better step than I was getting last night with no effect at all.

Any ideas?

jdMorgan




msg:3067950
 8:33 pm on Aug 31, 2006 (gmt 0)

Replace the broken pipe "¦" characters with solid pipe characters; Posting on this board modifies that character.

Jim

lcutting




msg:3068284
 1:06 am on Sep 1, 2006 (gmt 0)

I did that last night but before I implemented the .htacess file.

Any other ideas?

Thanks,

jdMorgan




msg:3068297
 1:30 am on Sep 1, 2006 (gmt 0)

What's your server error log say?

Jim

lcutting




msg:3068332
 2:27 am on Sep 1, 2006 (gmt 0)

RewriteCond: bad argument line '%{HTTP_REFERER}!^$'!

jdMorgan




msg:3068730
 12:37 pm on Sep 1, 2006 (gmt 0)

A missing a space between "}" and "!"?

You could just as well write it this way:

RewriteCond %{HTTP_REFERER} .

Jim

lcutting




msg:3069000
 5:12 pm on Sep 1, 2006 (gmt 0)

I still get

RewriteCond: bad argument line '%{HTTP_REFERER}'!

jdMorgan




msg:3069025
 5:40 pm on Sep 1, 2006 (gmt 0)

You left off the space-period at the end, apparently.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved