Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
</Location>

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
add comment

2 Answers

Yep, with <Files> (or <FilesMatch>): http://httpd.apache.org/docs/2.2/mod/core.html#files

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
2  
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
add comment

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
1  
<Location> tags are not accepted in htaccess, it's context is "server config, virtual host" httpd.apache.org/docs/current/mod/core.html#location –  dimvic Mar 16 '12 at 16:50
add comment

Your Answer

 
discard

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.