Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Basic Authentication (htaccess) to restrict access to a specific URL

I need to restrict access to a particular URL, e.g. http://mydomain.com/this/is/the/url on my webserver using Basic Authentication through Apache. Any other URL should be openly accessible. I have seen that you can add specific rules to files using:

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

My problem is that all requests are routed to controllers using mod-rewrite and so I don't think that I can restrict access based on the file. Any ideas would be most helpful!

like image 987
Matt Avatar asked Mar 23 '11 11:03

Matt


People also ask

How do I put a password on my URL?

The username and password must be added with the format − https://username:password@URL.

How do I password protect Apache with basic authentication?

The htpasswd command allows you to create a password file that Apache can use to authenticate users. You'll create a hidden file for this purpose called . htpasswd within your /etc/apache2 configuration directory. The first time you use this utility, you need to add the -c option to create the specified .

How do you use basic authentication?

Basic authentication is easy to define. In the global securityDefinitions section, add an entry with type: basic and an arbitrary name (in this example - basicAuth). Then, apply security to the whole API or specific operations by using the security section.


1 Answers

In .htacess file you should put :

AuthType Basic
AuthName "Need to login"
AuthUserFile .htpasswd file location ;
Require user USER

//AuthName is login prompt message
//AuthUserFile  is physical .htpasswd file location i.e.
C:/xampp/htdocs/basic/.htpasswd
//Require user is for a specific user i.e. the username you want to
authenticate

To generate .htpasswd file you can use : - http://www.htaccesstools.com/htpasswd-generator/

like image 56
Musleh Uddin Avatar answered Oct 09 '22 05:10

Musleh Uddin