How to allow or deny ip addresses using .htaccess

The file .htaccess always comes useful whenever we need more protection for our website. Among all the capabilities of .htaccess we are here going to discuss it capability of allowing or denying IP address. I need this function because there are some directories on my server which are critical to my business and hence want to protect these from evil eyes.

Let us say you have a directory HOME which you want to protect. Just follow the instructions given below to protect the given directory:

  1. First of all create a new file in the given directory and name it as .htaccess
  2. Open this file in any editor.
  3. Write this line the newly created file deny from all
  4. After than write allow from xxx.yyy.zzz.qqq
  5. In the above statement xxx.yyy.zzz.qqq is the IP address which you want to allow
  6. In case you want to allow a range IP address than simply leave the block which is dynamic or variable in your ip address. For eg.
  7. allow from xxx.yyy
  8. In case you just want to deny just one IP address, than simply write deny from xxx.yyy.zzz.qqq

Though this method is perfect for blocking  individual ip addresses but it is not efficient enough to block a particular range of IP address. By efficient i mean that we cannot make a particular range of IP address to block using this method

How to perform redirection from www to non-www version of the domain or vice versa

Performing redirection fom non www to www or essential is a essential for SEO of a particular site because most of the search engines treat both www and non-www version differently and hence regard a single page as duplicate of other.

Search engines than put penalty on  duplicate content  in spite of the fact that your article is original and genuine.

This can easily be avoided by using .htaccess file in the root of your Website.

NOTE: This method is applicable only to those servers which allow mod rewrite in Apache(Linux) or IIS(Windows). Please confirm with your host.

Add following lines to redirect all www request to non-www version of your domain

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

Add following lines to redirect all www request to non-www version of your domain

RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

In above two modification we use 301 redirect which means that a given page is permanently moved from this location thus telling search engines not to take this page as duplicate copy of other.