Powered By Blogger

Monday, 12 March 2012

Protecting Your Pages with htaccess

When you create a .htaccess file, all of the files within that directory, as well as
any subdirectories, are protected by that .htaccess file. Any .htaccess files above
 that directory are also used.
You can control access to specific files or specific types of files by using wildcards or
 filenames in the opening <Files> tag of the .htaccess file.
Use multiple <Files> tags in a single .htaccess file to restrict files with different access controls.

Example of .htaccess content to limit the access of the website to Local Area Network only.

 # limit access to local area network only
<Limit GET POST PUT>
 order deny,allow
 deny from all
 allow from 192.168.151.0/24
# This will limit the access only from 192.168.151.0 to 192.168.151.255
</Limit>

 To add password protection to your pages, you need to do the following two things:

  1. Create a text file on your server that will store your username and password.
  2. Create a special file called .htaccess in the folder you want to protect.

1,Creating the password file

The first step is to create a simple text file that will store your username and password, separated by a colon (:). The small catch is that the password must be encrypted. Luckily, there are many free web-based utilities that will encrypt the password for you. Try one of these:
Simply enter your desired username and password in one of these pages and submit the form. You'll get back a string similar to the following:


2,Creating the .htaccess file


you need to put the following code in your .htaccess file:

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Folder"
Require valid-user
 
 
/full/path/to/.htpasswd should be the full path to the .htpasswd file that you uploaded earlier. The full path is the path to the file from the Web server's volume root - for example, /home/username/.htpasswd or C:\wwwroot\username\.htpasswd. (If you're not sure of the full path to your site or home directory, ask your Web hosting company for this info.)
The above .htaccess file will password protect all files in the folder that it is placed in, and all sub-folders under that folder too. So if you wanted to password protect your entire site, you would place the .htaccess file in your Web root folder.

Protecting a file

To password protect just a single file in a folder, use the following .htaccess file:

AuthUserFile /full/path/to/.htpasswd
AuthType Basic
AuthName "My Secret Page"

<Files "mypage.html">
  Require valid-user
</Files>

This will password protect just the mypage.html file in the folder where you put the .htaccess file.

No comments:

Post a Comment