I started a small tutorial about How to use htaccess file for your website. Here is the second part of htaccess tutorial.
Deny visitors by IP address
The visitor blocking facilities offered by the Apache Web Server enable us to deny access to specific visitors, or allow access to specific visitors with the help of htaccess. This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site, such as an administration area.
To set-up visitors restrictions and blocking, create a .htaccess file following the main instructions and guidance which includes the following text:
order allow,deny deny from 255.0.0.0 deny from 123.45.6. allow from all
The above lines tell the Apache Web Server to block visitors from the IP address ’255.0.0.0′ and ’123.45.6.’, note the second IP address is missing the fourth set of digits, this means any IP address which matches the firth three set of digits will be blocked, e.g. ’188.8.131.52′ and ’184.108.40.206′ would be blocked.
To set-up blocking of all visitors except yourself, create a .htaccess file following the main instructions and guidance which includes the following text:
order allow,deny allow from 255.0.0.0 deny from all
The above lines tell the Apache Web Server to block all visitors except those with the IP address ’255.0.0.0′, which you should replace with your own IP address.
You may add any number of ‘deny from’ and ‘allow from’ records after the ‘order allow,deny’. Note the change from ‘allow from all’ to ‘deny from all’ on the bottom line, this is important and must be changed depending on your requirements. If you want to allow your visitor access, you would use ‘allow from all’ and place ‘deny from’ lines above.
Blocked visitors will be shown a ’403 Forbidden’ error message. You can customise this error message by following the ‘Error Documents’ section of this article.
How to redirect Redirect URL?
Redirects enable us to direct web site visitors from one document within your web site to another. This is useful for example, if you have moved your web site content and would like to redirect visitors from old links to the new content location.
To set-up redirects, create a .htaccess file following the main instructions and guidance which includes the following text:
Redirect /old_dir/ http://www.yourdomain.com/new_dir/index.html
The above line tells the Apache Web Server that if a visitor requests a documents located in the directory ‘old_dir’, then to display the document ‘index.html’ located in the directory ‘new_dir’.
You see in this example, the ‘old_dir’ is the location of the document to be requested by the visitor, and is a document or directory located under your main domain. In this example, the directory ‘old_dir’ would be located at ‘http://www.yourdomain.com/old_dir/’. However, you will also notice the location of the file that the visitor is to be redirected to is a full web site URL, not what is referred to as a relative URL in the case of ‘old_dir’. This means we can redirect visitors to the ‘old_dir’ folder to any web site document, it doesn’t have to be held within your web site content and could be any web site.
It is very important (and the most common cause of error) that you understand the difference between a relative URL and an absolute/full URL. A relative URL is the location of the document within the web site, and does not include the actual domain name of the web site. These are used for documents held within the web site to simplify and shorten the URL. A absolute or full URL is one which includes the full domain name.
For example, for a absolute/full URL, ‘http://www.yourdomain.com/directory/file.html’. the relative URL for this document would be, ‘/directory/file.html’.