How to add password protection to your website domain with htaccess?

This example is very simple and easy to use. If we want a domain or directory on the web to be password protected i.e. to be only used by providing username and password, we can use the below code in doing so.

1. Create a “.htaccess” file on the root of the website or the directory.
2. Copy and paste the following code in your .htaccess file

AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/xxx/public_html/.htpasswd
require valid-user

3. Now, create a file with the name “.htpasswd” (at the same location as mentioned in the step 2, AuthUserFile)
4. Copy & paste the following code in your .htpasswrd file


5. You’re done! Now, when you try to access your domain where the above steps has been applied, it will ask for your username and password.

Here the username is developer and the password is test1



Yuvraj has written 39 articles

"Learn & Share" - I believe in this. What about you?