The.htaccess file is a configuration file that Apache-based web servers use. In .htaccess files, you can use directives to redirect requests to different URLs, control directory listings, specify custom error documents, and more. This article will show you how to protect directories, subdirectores using a code snippet in .htaccess.To learn more about how to use .htaccess files, click on this link.
You can protect files , directories and subdirectories to prevent unauthorized access.Following are the parameters used in the code to protect the folders.
To protect the main web directory , add the following code to the .htaccess file:
#Protect Directory AuthName "Dialog prompt" AuthType Basic AuthUserFile /home/username/example.com/.htpasswd Require valid-user
To protect a subdirectory add the following code to the .thaccess file. The example shows you how to protect the subdirectory named members folder.
#Protect Directory AuthName "Dialog prompt" AuthType Basic AuthUserFile /home/username/example.com/members/.htpasswd Require valid-user
When a user tries to access a password-protected folder, Wordpress throws a 404 Not Found. To avoid this, add the ErrorDocument 401 default line, which will result in a “401 Unauthorized” response for site visitors.
ErrorDocument 401 default #Protect Directory AuthName "Dialog prompt" AuthType Basic AuthUserFile /home/username/example.com/members/.htpasswd Require valid-user
To view an online tutorial for using .htaccess files, please visit http://httpd.apache.org/docs/2.2/howto/htaccess.html.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.