Forum Moderators: phranque
I need an htaccess file to allow a specific site to hotlink to everything in a directory ( css, js, png, jpg, jpeg, mov, swf)
But I do not want someone to go to the url and access the same files via browser.
So far I have this - any suggestions would be great :)
SetEnvIfNoCase Referer "^http://www.mysite.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://www.mysite.com$" locally_linked=1
SetEnvIfNoCase Referer "^http://mysite.com/" locally_linked=1
SetEnvIfNoCase Referer "^http://mysite.com$" locally_linked=1
SetEnvIfNoCase Referer "^$" locally_linked=1
<FilesMatch ".(css¦js¦gif¦png¦jpg¦jpeg)$">
Order Deny,Allow
Deny from env=locally_linked
</FilesMatch>
RewriteEngine on
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?site-allowed-to-link\.com [NC]
RewriteRule \.(css¦js¦png¦gif¦jpg¦jpeg)$ - [F]
SetEnvIfNoCase Referer "^https?://(www\.)?mysite\.com" allow_link
SetEnvIfNoCase Referer "^https?://(www\.)?site-allowed-to-link\.com" allow_link
SetEnvIf Referer "^$" allow_link
#
<FilesMatch "\.(css¦js¦gif¦png¦jpe?g)$">
Order Deny,Allow
Deny from all
Allow from env=allow_link
</FilesMatch>
Jim
The other is more fundamental, and that is that the referrer header is not required in HTTP, it is optional. Therefore, internet security software, and corporate and ISP caching proxies (used by all AOL and Earthlink customers for example) do not send the HTTP Referer header with their requests.
When a URL is typed-in, or requested via JavaScript in Internet Explorer (specifically), there is no referer. SImilarly, when a search engine robot requests your pages, there is no referer.
Therefore, you are asking for a solution to an impossible problem if the solution is based on the HTTP Referer header: You must allow blank referrers for your images to load (even for your own site) for a significant number of users who due to their ISP, will send no referer, and likely also for search engines which send no referer, and yet you don't want to allow type-ins, which also won't have a referrer.
The code I posted is dirt-simple, works on tons of other servers, and should work for all other "unwelcome hotlinking sites", assuming that you replaced the broken pipe characters as advised above. However, you must flush your browser cache, or your browser will simply show you its cached version of the page and server response headers, until whatever expiry time you have set has passed. If you have not configured your server to send Cache-Control and Expires headers, then your browser will show you the cached pages and responses until that cache entry is so old that it is replaced with a new page, based on typical oldest-cache-slot-first replacement in the browser.
Now, back to the original problem. As g1smd stated, and as I re-stated, you've got a conundrum: You want to block blank referrers from some sites and from type-ins, and yet you don't want even your own site to appear broken for users who for one reason or another send no Referer header, or have it blocked unbeknownst to them.
One way out of the sack is to use scripting to set a cookie on "authorized pages" of your site, and to check that cookie before serving any of your restricted content-types. If the cookie isn't set, then return a 403-Forbidden or blank or alternate content.
But this will take some special work-around to allow that other site to hotlink your stuff. Because cookies are only visible to the domain that serves them, I don't see a good solution for that right now.
Jim
Jim
If in my code i just put this (below) with just css it allows the css to pull the images but then you can type in the url to the images. So I guess there is a problem with css and this type of deny allowing.
<FilesMatch "\.(css)$">
Order Deny,Allow
Deny from all
Allow from env=a
To speed up the code, get rid of the redundancies:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?linking-site\.com [NC]
RewriteRule \.(gif¦jpe?g¦bmp¦css¦png¦js)$ - [F,NC]
Jim
Why would it treat different file types differently?
Why would it treat different file types differently?
The most likely cause is that image requests sent by the browser when requesting CSS backgrounds are not sent with a referrer.
Use the Live HTTP Headers add-on for Firefox/Mozilla-based browsers to investigate this behavior with Mozilla-type browsers. I'm not aware of any equivalent plug-in or tool for IE, and it wouldn't surprise me if IE's CSS-image-fetching behavior was different, either.
As I'm quite sure you can't get a reliable referrer-based solution to your problem, I can only advise you that you're wasting your time with this approach, and if this is really important to you, then you need to look into more-reliable script-based methods, as described above.
Good luck,
Jim