APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Using .htaccess files to restrict access

© May 2019 Anthony Lawrence

Every directory in a Apache website can have an optional .htaccess file. This can be used to require passwords to access the files therein or to redirect requests to other pages. Everything you can do in an .htaccess file can also be done in your web server's configuration file, but that can be a little intimidating and confusing for new web masters. The .htaccess is slightly less efficient, but easy to use.

Restricted Access

Let's say you want to restrict access to a certain subdirectory. Create .htaccess in that directory, and put this into it:

 AuthUserFile /www/data/.htpasswd
 AuthName "Login and Password Required"
 AuthType Basic
 <Limit GET>
 require valid-user

Note that AuthUserFile can be anywhere that the web server can read. You then have to create the .htpasswd file:

 htpasswd -c /www/data/.htpasswd username

That prompts for a password for "username" and that's it. You can add additional users with the same command; just leave off the "-c".

At some sites, we tell the user what name to use and they only have to remember the password. You can even prompt them or give them hints in the Authname string if that helps.

It's that easy. Tomorrow I'll cover using .htaccess to redirect requests and to help with security.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Using .htaccess files to restrict access

Inexpensive and informative Apple related e-books:

iOS 10: A Take Control Crash Course

Take Control of Numbers

Take Control of Apple Mail, Third Edition

Take Control of Parallels Desktop 12

Photos for Mac: A Take Control Crash Course

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

Printer Friendly Version

Software engineering is the part of computer science which is too difficult for the computer scientist. (Friedrich Bauer)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode