Basic Authentication Using .htaccess In Wamp/Xampp Server

This is how we create a basic authentication using .htaccess file in windows. The use case is when you provide a simple api to your client. This will secure the url with user and password using basic auth. Accessing the path and any files  below url user must authenticate first.

http://localhost/secure/

Following the steps below we will have the .htaccess and .htpasswd inside \secure folder. This folder is in your local web server


1. Create the .htaccess file in folder that you want to secure (in this example secure/ folder). The .htaccess file should contain the 4 lines below
   AuthType Basic
 AuthName "Restricted Content"
 AuthUserFile D:/wamp/www/secure/.htpasswd
 Require valid-user


2. To create the .htpasswd file, using command line locate the bin directory (where htpasswd.exe is located) in the active version of apache (D:\wamp\bin\apache\apache2.0.63\bin). Run

 htpasswd -c D:\wamp\www\secure\.htpasswd user


should look like below

D:\wamp\bin\apache\apache2.0.63\bin>htpasswd -c D:\wamp\www\secure\.htpasswd user

where:
-c = option to create a file  D:\wamp\www\secure\.htpasswd This is define in .htaccess  in #1
user = is the username

This will prompt you to input the password twice. Then it will create the .htpasswd file in the given path. Accessing http://localhost/secure/ in the browser will prompt you the user password authentication

Comments

Popular posts from this blog

Prevent parent anchor tag to redirect when a child is clicked