Search code examples
phpcss.htaccesshotlinking

Disable stylesheet based on referrer


I don't want to allow the stylesheet (hosted with me) to be loaded on a couple of domains.

This is what I'm trying, but doesn't work:

RewriteEngine on

RewriteCond %{HTTP_REFERER} ^http://example1.com/ [NC]
RewriteRule \.css$ http://www.mywebsite.com/dummy.css [R,L] 

RewriteCond %{HTTP_REFERER} ^http://example2.com/ [NC]
RewriteRule \.css$ http://www.mywebsite.com/dummy.css [R,L] 

How can I make it work?


Solution

  • css/.htaccess

    Options +FollowSymLinks
    RewriteEngine On
    
    # Redirect requests to load.php
    RewriteCond %{REQUEST_URI} .*\.css$ [NC]
    RewriteRule .* load.php
    

    css/load.php

    $referrer = $_SERVER['HTTP_REFERER']; 
    
    if (strpos($referrer, 'allowedwebsite1.com') !== false or 
        strpos($referrer, 'allowedwebsite2.com') !== false ) {
    
        header("Content-type: text/css", true);
        $css = file_get_contents("style.css");
        echo $css;
    } 
    else {
        header("Content-type: text/css", true);
        echo ''; // empty stylesheet
    }