Protection contre le hotlinking avec Varnish

Voici un exemple de condition à ajouter dans votre vcl recv pour protéger un dossier d’un site :

if (req.http.host == "le.domaine.a.proteger.ltd" && req.url ~ "^/l/uri/a/proteger" && (req.http.referer && req.http.referer !~ "^http://le.domaine.a.proteger.ltd/")) {
    error 403 "No hotlinking please";
}

Remplacer :

  • le.domaine.a.proteger.ltd par le domaine (attention si votre site est accessible avec et sans www)
  • /l/uri/a/proteger : par l’uri à protéger, supprimer cette partie si vous voulez protéger l’intégralité du site

Pour mettre en place le même type de protection via apache, voir cet article : Un htaccess pour les pompeurs de sites dans lequel est expliqué comment procéder avec des règles de réécriture Apache

Un htaccess pour les pompeurs de sites

De manière globale en retournant un accès interdit

SetEnvIfNoCase Referer "^http://notes.depad.fr/" local_ref=1
Order Allow,Deny
Allow from env=local_ref

Ou de manière plus subtile en y mettant plusieurs conditions

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?notes.depad.fr(/)?.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www.)?google.(fr|de|com|ca)(/)?.*$ [NC]
RewriteRule .*.(gif|png|jpg|jpeg|pdf|doc|wav|bmp|avi|mpg|mpeg|mp3|zip|rar)$ /nohotlink.png [R,NC]

Si vous avez mis en place un cache Varnish en amont, consulter cet article : Protection contre le hotlinking avec Varnish