Forum Moderators: phranque
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteRule \.(gif¦jpe?g¦png)$ - [NC,F]
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mydomain\.com [NC]
RewriteCond %{REQUEST_URI} !^/alternate_image\.
RewriteRule \.(gif¦jpe?g¦png)$ /alternate_image.$1 [L]
However in a few cases, it turns out that some hosting providers don't trust their users or can't provide support for the (admittedly) potentially-complex problems that can arise when using mod_rewrite. Many low-cost hosting providers simply can't afford to support such a compact, complex, powerful 'language' as mod_rewrite, especially since it relies on another compact, complex, powerful element -- Regular Expressions [etext.lib.virginia.edu].
For those Webmasters stuck with such a situation, there is another possible simple solution, and that is to use mod_setenvif [httpd.apache.org] and mod_access [httpd.apache.org] instead of mod_rewrite. In many cases, hosting providers that don't support mod_rewrite *do* allow users to use these other modules. Mod_setenvif is available only on Apache 1.3 and later, and there are several restrictions on mod_setenvif previous to Apache 1.3.13 -- See the Apache mod_setenvif documentation cited above for the (numerous) details.
The following code accomplishes exactly the same thing as the first mod_rewrite code example above, and is the best you can do with mod_setenvif and mod_access; The option of serving an alternate image can't be supported by these modules.
<FilesMatch "\.(gif¦jpe?g¦png)$">
SetEnvIfNoCase Referer ^$ allow_image
SetEnvIfNoCase Referer ^http://(www\.)?mydomain\.com allow_image
Order Deny,Allow
Deny from all
Allow from allow_image
</FilesMatch>
The above examples are just that - examples. They may contain typos. They are certainly not suitable for all possible applications, and you should expect to need to modify them to suit your specific needs. Time spent studying the documentation linked above is worthwhile.
If none of the above anti-hotlinking solutions are supported by your server or adequate for your needs, there remain the options of using cookies to control access or redirecting all image requests to a PERL or PHP script for processing.
Jim