You've got two choices: <Location>
or <Directory>
in the server config (they can't be placed in .htaccess), or a .htaccess
file
<Location /admin>
Order Deny,Allow
Deny from All
Allow from your.ip.addr.ess
AuthType Basic
AuthName "Admin page. Keep out"
AuthUserFile /some/path/htpasswd
AuthGroupFile /dev/null
Require valid-user
</Location>
Location
allows you to restrict by URL, which will match regardless of where the files are physically stored on the server, but also can be bypassed if you allow symlinks to be followed. Directory
works the same way, but matches by physical server-side path, regardless of how the directory/file is accessed. With Location
, you can specify an absolute path, including a file, to match on, if you want to restrict just the one file.
With .htaccess, you're essentially duplicating the Directory
directives, but can do so without having to bounce the server to load the new configuration, at the cost of the .htaccess having to be parsed for every request.
It's best to not rely on password protection alone to secure administrative scripts, so I've added the IP address (Order/Deny/Allow directives) filter as well. Better security yet is to place the admin scripts on a completely seperate domain (even if it's still hosted on the same physical server) so someone poking at random URLs on the main site won't find the adminstrative section.