Suppose I have a home directory "~someuser/www" which Apache (maintained by someone else than me) exposes as http://www.myserver.com/~someuser . If I want to limit this directory to certain users, I can create a .htaccess file in the www folder containing the line "Require user username1 username2" etc.

The question is: Is there a way to specify in .htaccess that only the owner of the home directory, whoever that might be, is supposed to be able to access it from the web?

If I knew that the owner was "someuser", then I would just specify "Require user someuser", but in this case I'm trying to put together a template that will work for a lot of users, and I'd like to avoid having to tell them each to insert their own username if possible.

(More background: Specifically I'm trying to get it to work on the MIT site described here: http://scripts.mit.edu/faq/15 . We'll have students doing little web projects in their own www folders, but don't want other students to be able to view their JavaScript source code.)


This is a cop-out, but make the students do it:

Make your first-day-getting-familiar-with-the-lab exercise something that gets this kind of housekeeping set up, explaining what the .htaccess file is, and linking the students to the .htaccess tutorial. Then as one of the exercises have them restrict web access on some directory to themselves, the TAs and the professor.

This is what I've traditionally done for the first-year programming classes I occasionally run at my alma mater ("Set the permissions on your home directory so people can't snoop.", "Configure CVS/git/whatever for your account." , etc...) - It saves me the work of having to do it, and makes the students get comfortable with the environment they'll be working in.

  • You can probably also do some hackery with SetEnv and URL parsing (though I don't know that you can do it in .htaccess - it may need to be done in the main apache config file) -- I would actually have to sit down and play to figure that out though. It strikes me as "Probably possible, but definitely work." – voretaq7 Sep 6 '11 at 21:17
  • Not unreasonable. In the end I created the file with the right user name as part of a script that they had to run to set up their system in the first place. Thanks! – eirikbakke Sep 8 '11 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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