Creating htpasswd / htaccess restricted areas

I have to do this all the time. You’ve created a staging site for a client, you want to show it to them on a live server – but you don’t want it to get indexed by search engines, nor do you want people accidentally coming across it – you want to use htaccess and basic auth to restrict the directory.

I almost always google the syntax for both:

  1. the htpasswd command.
  2. the htaccess syntax for adding auth.

It’s annoying, this shouldn’t be this hard to remember (but it is). So, in a moment of free time I wrote a really simple shell script that does all of this in one command for me. I’m calling it htpassmaker.

This is it, in a gist:

Assumptions I’ve made:

  1. You’re using apache.
  2. You have htaccess files enabled on your server.
  3. You have shell access to your server.

Things to remember:

  1. Put your htpasswd file OUTSIDE you webroot. I find the best course of action is to put htpassmaker in your unix path, then run it from the directory above where your webroot is, once complete, copy the created “.htaccess” into the webroot (or append it to the existing htaccess if you have one)
  2. I take no responsibility for this.

It’s unlikely, but if you use this (or think it’s stupid) let me know in the comments, or on twitter @soyrex.