Page is a not externally linkable
- Code, Content, and Presentation
-- Apache Web Server
---- attempt to block hot-linking actually blocks everything


jdMorgan - 5:35 am on Sep 21, 2002 (gmt 0)


stapel,

Your regular expressions need a few tweaks...

#Prevent hot-linking of images:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain1\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?domain2\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^http://216\.239\.(3[2-9]¦[45][0-9]¦6[0-3])\..*(www)?domain1\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://216\.239\.(3[2-9]¦[45][0-9]¦6[0-3])\..*(www)?domain2\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://babel.altavista.com/.*(www)?domain1\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://babel.altavista.com/.*(www)?domain2\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://216\.243\.113\.1/cgi/
RewriteCond %{HTTP_REFERER} !^http://search.*\.cometsystems\.com/search.*(www)?domain1\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://search.*\.cometsystems\.com/search.*(www)?domain2\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://.*searchhippo\.com.*(www)?domain1\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://.*searchhippo\.com.*(www)?domain2\.com [NC]
RewriteRule \.(jpeg?¦jpg¦gif)$ [domain1.com...] [NC,R,L]

If you have mod_rewrite installed (and I didn't add any typos), this should work.

Note that if you'd rather use a .gif file as your replacement image, you can do so by adding one more RewriteCond:

RewriteCond %{REQUEST_URI} !^/hotlink\.gif$

In other words, once you've redirected to the "buzz off" graphic, you don't want to block it by redirecting again - this would cause an infinite loop, and likely take your server down!

The extra lines I added are to allow Google, GigaBlast, SearchHippo, AltaVista, and CometSystems search engines to properly display their translated and/or cached versions of your site. If you don't want them to be able to display your images with your translated/cached page, just remove those lines. Leaving them in will mean you'll need to keep an eye on these lines in case the search engines change IP addresses or cgi paths in the future. However, you'll be no worse off with incorrect addresses in those lines than if you leave them out entirely now...

You can also save yourself a lot of duplicate RewriteConds (and work) if you are willing to pick a "standard" URL and stick with it. Simply redirect any incoming requests for the "non-standard" domain names to the "standard" domain name. For example:

RewriteCond %{HTTP_HOST} !^www\.domain1\.com$
RewriteRule ^(.*)$ [domain1.com...] [L,R=permanent]

This would "correct" the URL in any browser or search engine that followed a "non-standard" domain link, and eliminate the need to handle all four domain name variants as was necessary in the rewrite rules above. Users would then only be bookmarking your "standard" domain name, and search engines would list only that "standard" domain name, "concentrating" the Google page rank in one listing. I don't know if you've been following the "multiple domain names, one site" threads here recently, but I prefer to just have one domain name "out there" per site and get all incoming links pointing to that one.

Also, this rewrite takes care of all the upper/lowercase variants as well, eliminating the need for all of those [NC] flags in the rewrite rules above.

Just those two lines alone would make a pretty good test to make sure that you host is allowing you to use mod_rewrite; Put them into .htaccess, along with the "RewriteEngine on" directive, and then use a different domain to access your site - e.g. leave off the "www.". You should see the URL in your browser address bar update to show the "standard" URL which includes the "www." in this example.

For more info, see the Apache mod-rewrite documentation [httpd.apache.org] and this handy regular expressions guide [etext.lib.virginia.edu].

Hope this helps,

Jim

[edited by: DaveAtIFG at 4:28 pm (utc) on Sep. 23, 2002]
[edit reason] Changed to "generic" URLs [/edit]


Thread source:: http://www.webmasterworld.com/apache/33.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com