This document demonstrates the use of .htaccess to restrict access to web documents by user and password.
How can I block people from certain IP Address so they can't access my site?
You can add the following to the .htaccess file in your public_html directory:
order deny,allow deny from 192.0.2.123
(change the ip address to the offending ip)
This will block them from seeing not only your main pages, but all pages in all subdirectories. If you wish to block them from seeing only one, or a small set of, subdirectories, place the above code in the .htaccess file of the highest subdirectory that you want blocked.
Can I limit access for a directory?
All new accounts have this capability.
You can find a list of features for each currently available account type here:
The Restricted Access Document
The document "Restricted Information" is only available to user john with password orange and user sheri with password apple. The first time you click on "Restricted Information", your web browser should prompt you for a user name and password. If it does not you may be using a web browser which is not HTTP authentication capable.
NOTE: Please make sure you are setting this up in the directory that you want passwd protected. The .htpasswd file will end up in the wrong directory otherwise. For more infomation on how to use Unix, please check out our Unix Tutorial.
- The web document to be protected - Actually, access is restricted by directory so all files in the same directory will be protected.
- The file .htaccess - This file should be in the directory which contains the documents to restrict access to. The contents of this file specify the name of the password file. For example if you were user jdoe with web documents in the local directory "/home/jdoe/public_html/goodstuff" that you wanted to restrict access to, your .htaccess might look like:
AuthUserFile /home/jdoe/public_html/goodstuff/.htpasswd AuthName jdoe_goodstuff AuthType Basic Require valid-user
- Note that .htaccess will not work if there are extra spaces after AuthUserFile.
- The file .htpasswd - This file contains the passwords of the users. To create the .htpasswd file log in to this server using SSH, change directory to the directory you want to restrict access to, and type:
htpasswd -c .htpasswd someuser
for the first user (where someuser is the username). You will then be prompted twice for the user's password. The -c option causes the .htpasswd file to be created. For each additional user type:
htpasswd .htpasswd someuser
NOTE: There is no correspondence between the usernames and passwords used for accounts on this server and usernames and passwords in any specific .htpasswd file. A user doesn't need to have an account on this system in order to be validated for access to files protected by HTTP-based authentication.