Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have used code like this in apache configuration to protect locations with password

<Location ~ "/admin.*">
    AuthType Basic
    AuthName "Protected Area"
    AuthUserFile /home/user/public_html/.htpasswd
    Require valid-user

is there a way to do the same thing using an htaccess file?

the locations I want to protect don't really exist on the filesystem, it's locations available thanks to mod_rewrite

share|improve this question

Yep, with <Files> (or <FilesMatch>):

Edit: "Note that unlike Directory and Location sections, Files sections can be used inside .htaccess files. This allows users to control access to their own files, at a file-by-file level."

share|improve this answer
I'm not sure this does what dimvic is looking for -- A <Location> directive can point to something that's not on the filesystem (e.g. <Location /server-status>) in which case .htaccess wouldn't work. It's probably viable if the area to be protect "really exists" though... – voretaq7 Oct 21 '10 at 19:35
I suspect they are files. – Mark Wagner Oct 21 '10 at 20:10
the area doesn't really exist, this is the problem, i need to protect an area that exists thanks to mod_rewrite – dimvic Oct 22 '10 at 12:44
If the area doesn't exist (on the filesystem) then there is no place to put an .htaccess file so what you are asking is impossible. – Mark Wagner Oct 22 '10 at 17:34
I was thinking that it could possibly be done with an htaccess placed in the site's root folder – dimvic Mar 16 '12 at 16:53

Yes it's possible to do so put a htaccess in the file you want to moderate the access. Put the same text as in the configuration file. Make sure that your conf files will read .htaccess.

share|improve this answer
I meant add it to one of directory that you need to moderate. – Gopoi Oct 21 '10 at 18:48
<Location> tags are not accepted in htaccess, it's context is "server config, virtual host" – dimvic Mar 16 '12 at 16:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.