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'm trying to pwd protect only my home page with apache basic auth.

This seems to work on everything not just the hp

<LocationMatch "/">
  AuthType Basic
  AuthName "heelooo?"
  AuthUserFile /var/path/.htpasswd
  Require valid-user
 </LocationMatch>

Changing the regex to ^/$ or ^$ makes it not authenticate anything. Any tips? (its running a mod_ruby rails app)

share|improve this question

2 Answers 2

It's always better to use Directory directive for setting authentication.

Location directives should not be used to control access to filesystem locations

<Directory /home-path>
AuthName Members
AuthType Basic
Require valid-user
AuthUserFile /auth-file-path/.htpasswd
</Directory>

Ruby-on-rails solution:

before_filter :authenticate

protected

def authenticate
  authenticate_or_request_with_http_basic do |username, password|
    username == "user" && password == "pass"
  end
end
share|improve this answer
    
hm, its all virtual (mod_ruby, rails) so I dont think I can specify a file like that since I don't have one. Well, tried it with the root dir without a / and its protecting everything instead of just the home page. –  zeedre Nov 7 '12 at 22:37
    
    
maybe a last resort. I'd rather not polute my codebase with conditionals for basic authentication. Is this not doable with pure apache? –  zeedre Nov 7 '12 at 23:29
    
It can be done from apache level but you'd need to run ruby on rails via fastcgi. Please note that apache has a very poor (performance wise) implementation of basic authentication when AuthUserFile is used (on each request AuthUserFile is read), so probably coding it yourself would be much more easier and more efficient. –  FINESEC Nov 7 '12 at 23:45
    
s/always/often/ –  andol Nov 7 '12 at 23:53

I reran all my attempts and one decided to work. I found that LocationMatch "^/$" does in deed work. I must have been doing something wrong before. basicauth can indeed work with mod_ruby - don't need to run as fastcgi. Thanks for all the help.

share|improve this answer

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.