Tutorials

Protect your pages through a .htaccess file

When your website is hosted on a Unix or Linux server, you can use a .htaccess file to prevent direct linking/leeching and block certain ip adresses.

  1. Open a new document in a text editor like Notepad and save it as .htaccess (nothing more, nothing less) in the folder you want to protect. For instance the folder where you keep your images. Place the images from your layout outside that folder, because it might affect your index page.

  2. Place the following text in the .htaccess file, so people can't read it:
    order allow,deny deny from all
  3. Use this code to prevent direct linking, if people do so a broken image will appear on their screen. Don't forget to change yourdomain.com in the name of your domain (in my case it would be kao-ani.com)
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www.)?domain.com/.*$ [NC]
    RewriteRule .(gif|jpg)$ - [F]
  4. If you would like people to see a no direct linking image, make one (or use the picture below) and call it linking.gif
    Place it in the main directory of your website and add the next line to the previous code:
    RewriteRule .(gif|jpg)$ http://www.domainname.com/linking.gif [R,L]
  5. you can use someones ip adress to keep them away from your website, if they are giving you problems. Banned users will get a 403 "forbidden" error. However, this person could still access your website from another computer.
    order allow,deny
    deny from 123.456.78.90
    allow from all
  6. This is a small list of bots that you might want to keep away from your site. They will crawl and copy your pages, which can increase your traffic a lot. Please visit this page for a more complete list.
    RewriteCond %{HTTP_USER_AGENT} Wget [OR]
    RewriteCond %{HTTP_USER_AGENT} CherryPickerSE [OR]
    RewriteCond %{HTTP_USER_AGENT} CherryPickerElite [OR]
    RewriteCond %{HTTP_USER_AGENT} EmailCollector [OR]
    RewriteCond %{HTTP_USER_AGENT} EmailSiphon [OR]
    RewriteCond %{HTTP_USER_AGENT} EmailWolf [OR]
    RewriteCond %{HTTP_USER_AGENT} ExtractorPro
    RewriteRule ^.*$ X.html [L]