Use .htaccess to stop hotlinking & spam comments

When running a website or blog, you have to take care of certain issues such as hotlinking and requests without a referrer. You can use .htaccess file to define certain rules to protect and manage your website. If your website does not have a .htaccess file, create a new text file and name it as .htaccess without any other extension.

Stop hotlinking using .htaccess

Hotlinking happens when another website uses uses your images, videos or other files directly from your server. Why is it bad? It puts unnecessary strain on your server. Its resources are being used for the benefit of another person while leaving your own visitors waiting for files to load.

# HOTLINK PROTECTION

 RewriteCond %{HTTP_REFERER} !^$
 RewriteCond %{REQUEST_FILENAME} -f
 RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !/hotlink\-(01|02).gif$ [NC]
 RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?perishablepress\. [NC]
 # RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
 RewriteRule \.(gif|jpe?g?|png)$ http://perishablepress.com/wordpress/hotlink-02.gif [R,NC,L]

Prevent spam comments

Spammers send automated comments using bots. These requests do not have a referrer. All request for online content originating from Web browsers have a referrer data identifying the user agent such as Mozilla Firefox and Google Chrome. If a request to post comment comes without referrer data, it is a spam comment. You are better off by turning down such requests. The following code will refuse spam comments in WordPress.

# DENY ACCESS TO NO-REFERRER REQUESTS

 RewriteCond %{REQUEST_METHOD} POST
 RewriteCond %{REQUEST_URI} .wp-comments-post\.
 RewriteCond %{HTTP_REFERER} !.*perishablepress\. [OR,NC]
 RewriteCond %{HTTP_USER_AGENT} ^$
 RewriteRule .* - [F,L]

Premium WordPress Themes

Reliable Web Hosting

Smart Web Worker strongly recommends HostGator for your website/blog hosting. Sign up for web hosting at just $3.96/month. Use the coupon code "SMARTBLOGGER25" to get 25% discount on any hosting packages. Get an account with HostGator now!

2 thoughts on “Use .htaccess to stop hotlinking & spam comments

  1. Hello..
    Is there a limit for 301 redirection in htaccess file? I have a website with 400 static html pages (my html file names are not good) and I would like to rename my html file names according to the keywords
    Can I use a single .htaccess file ?

    Thank you

    • You can add as many 301 redirection rules. You can have only one .htaccess file in one folder. All the rules will go in there—bet it 1 or 1000.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>