Forum Moderators: open
addEventListener('fetch', event => {
event.respondWith(fetchAndApply(event.request))
})
async function fetchAndApply(request) {
let referer = request.headers.get('Referer')
// check if there is a user agent
if (request.headers.get('user-agent')) {
// if a search engine allow request
if ((request.headers.get('user-agent').includes('googlebot')) || (request.headers.get('user-agent').includes('msn')) || (request.headers.get('user-agent').includes('yandex'))) {
return fetch(request)
} else {
//check if there is a referer
if (referer) {
// It's an image and there's a Referer. Verify that the
// hostnames match.
if (new URL(referer).hostname ==
new URL(request.url).hostname) {
return fetch(request)
} else {
console.log('referer',referer)
// Hosts don't match. This is a hotlink. Redirect the
// user to our 404.
return new Response('Sorry, this page is not available at this time.', {
status: 404,
headers: {
'Location': '/404'
}
})
}
}
}
}
else {
console.log('referer',referer)
// `No Useragent. Redirect to
// 404
return new Response('Sorry, this page is not available at this time.', {
status: 404,
headers: {
'Location': '/404'
}
})
}
}
I use Cloudflare and have now added the following javascript to all pages...
Where exactly will this script be placed?
The Cloudflare bot crawls the original site with the image and caches it. Once done, you won't see that many referrers from the hot-linking site, since requests for that image will now be pointed toward the Cloudflare caching system.
@3zero your initial post was ambiguous you wrote:
I use Cloudflare and have now added the following javascript to all pages...
I use Cloudflare and have now added the following javascript to all pages using their new "Workers" feature and applied it to the image directory
The Cloudflare bot crawls the original site with the image and caches it. Once done, you won't see that many referrers from the hot-linking site, since requests for that image will now be pointed toward the Cloudflare caching system.
Code to remove existing cached images on cloudflare (can be removed after 1 month) for http.conf
<If "%{HTTP_REFERER} =~ /yourdomain/">
</If>
<Else>
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif)$">
Header set Cache-Control "private, no-cache, max-age=0"
Header set Pragma "no-cache"
</FilesMatch>
</Else>
The problem is blocking the hotlinking without preventing Google and other beneficial bots from accessing the images.
if ((request.headers.get('user-agent').includes('googlebot')) || (request.headers.get('user-agent').includes('applebot')) || (request.headers.get('user-agent').includes('bingbot')) || (request.headers.get('user-agent').includes('pinterest')) || (request.headers.get('user-agent').includes('facebookfacebookexternalhit')) || (request.headers.get('user-agent').includes('facebook')) || (request.headers.get('user-agent').includes('twitter')) || (request.headers.get('user-agent').includes('GoogleImageProxy')) || (request.headers.get('user-agent').includes('yandex'))) {
return fetch(request)
}