WordPress.org

Ready to get started?Download WordPress

Forums

How to stop hotlinking with www and without www (7 posts)

  1. atdblog
    Member
    Posted 2 years ago #

    Trying to block hotlinking for both "mydomain.com" and "www.mydomain.com". My code does not block hotlinking from "mydomain.com".

    My current .htaccess code is:

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !google. [NC]
    RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
    RewriteCond %{HTTP_REFERER} !msn. [NC]
    RewriteCond %{HTTP_REFERER} !yahoo. [NC]
    RewriteRule .*.(gif|jpg|png|ico)$ – [F,L]
    </ifModule>

    What can I add to block hotlinking images from "mydomain.com" without the "www"?

    Thanks!

  2. atdblog
    Member
    Posted 2 years ago #

    May have been confusing ... meant to say ...

    A blog cannot hotlink to my image at "http://www.mydomain.com/wp-content/uploads/2012/02/elephant_quote.jpg"

    But they can still hotlink to my image at "http://mydomain.com/wp-content/uploads/2012/02/elephant_quote.jpg"

    I appear to be missing some code in my .htaccess.

  3. atdblog
    Member
    Posted 2 years ago #

    Ok, the code above seems to be working today. I thought I had my cache cleared but I guess I didn't.

  4. atdblog
    Member
    Posted 2 years ago #

    Others may find this "stop hotlinking" code useful ...

    My root .htaccess file now looks like this:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?mydomain.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www.)?seconddomain.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !google. [NC]
    RewriteCond %{HTTP_REFERER} !search?q=cache [NC]
    RewriteCond %{HTTP_REFERER} !msn. [NC]
    RewriteCond %{HTTP_REFERER} !yahoo. [NC]
    RewriteCond %{REQUEST_URI} !^/pages/images/
    RewriteRule .*.(gif|jpg|png|ico)$ – [F,L]
    </ifModule>
    
    # END WordPress

    Where "mydomain.com" is my site, "seconddomain.com" is a site I allow to hotlink my pics, followed by allowing search engines to hotlink my pic, and finally a condition to allow anyone to hotlink pics from my non-WP "pages/images" directory.

  5. atdblog
    Member
    Posted 2 years ago #

    And if you replace this line
    RewriteRule .*.(gif|jpg|png|ico)$ – [F,L]

    with this line
    RewriteRule .*.(gif|jpg|jpeg|png|ico)$ http://www.mydomain.com/pages/images/hotlinkingisbad.jpg [R,NC,L]

    Anyone who attempts to hotlink will be served with an image (that you create) saying something like "hotlinking is bad, please save the image for your site". The image must reside in the directory where you allow hotlinking (pages/images in this case).

    Fyi: You can test your code to prevent hotlinking at a number of test sites, including this real-time editor http://techgyo.com/HTML-Editor.htm (remember to clear your cache, first and often).

  6. suzaindawn
    Member
    Posted 2 years ago #

    I am looking for solution of this difficult question. I guess i got it. Thanks for sharing.Austin rentall and Rental Apartments

  7. OmTamil
    Member
    Posted 2 years ago #

    Thanks for the code.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags