Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite: block hotlinkers yet ALLOW a particular image file

         

mineray

9:35 pm on Nov 24, 2003 (gmt 0)

10+ Year Member



Hi,

I am using mod_rewrite in my .htaccess file to block hotlinking of my images. This is working fine.

However, I need to ALLOW my logo image regardless, because I use it in my email signature and it's being blocked since web mail users and some email clients either don't pass a referrer or pass one that isn't on my domain.

How can I modify my .htaccess to allow unconditional access to my logo.gif file while still blocking hotlinking of all other images?

Below is the code I have:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/.*$ [NC]
RewriteRule .*\.(gif¦GIF¦JPG¦jpg¦png¦PNG)$ http://example.com/stop.gif

Thanks!

[edited by: jdMorgan at 9:54 pm (utc) on Nov. 24, 2003]
[edit reason] No specific URLs, please [/edit]

bcolflesh

9:37 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Easy solution - put your logo.gif in a separate directory not protected by .htaccess

jdMorgan

9:52 pm on Nov 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?example.com/ [NC]
[b]RewriteCond %{REQUEST_URI} !^/logo\.gif$[/b]
RewriteRule \.(gif¦jpg¦png)$ /stop.gif [NC,L]

mineray

11:39 pm on Nov 24, 2003 (gmt 0)

10+ Year Member



Both solutions should work great-- thank you!

Sidenote: I'm not sure what they are talking about, but my web host said that parenthesis around the filename extension list don't work on their Apache server (?). I assume they are wrong?

Thanks

jdMorgan

12:12 am on Nov 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mineray,

If you asked them to install the code and it failed, then the reason would be that posting on this forum changes the "¦" characters in the mod_rewrite code. In order to work, you must replace them with the solid vertical pipe character from your keyboard -- usually Shift-\

Sorry, I forgot to point that out.

If, on the other hand, their version of Apache is so old that it won't support POSIX regular expressions, you probably need a new host.

Jim