November 28, 2002

Magic userdir urls with Perl

I am still experimenting with this Movable Type thing. This is an attempt to determine two things. First, if I can do this with w3m, which does not support javascript - there seems to be a lot of javascript scattered about. Second, if I can do any decent html markup, or whether this will be painful. Anyways, this article is the same as something that I just posted on Advogato, and I just want to see if it will look right.

Note: This sort of worked via w3m, but I still had to go back to a javascript-enabled browser to get the page to actually show up here. Oh well.

------------

Folks on #apache (irc.openprojects.net) have frequently asked a question that goes something like this:

I know how to make the url http://foo.com/~user go to the user's home directory, but how do I make that happen without the ~, which looks unprofessional?

There are two standard answers to this. One, you tell them that it can't be done without creating an Alias for every user, which is technically correct. The other answer is to tell them to write some sort of magic handler that knows what users are out there, and maps requests to the correct directory. Perhaps in mod_perl?

Well, over the last few weeks, I have been doing a number of the things that I have been telling folks on #apache to do, or things that I have long claimed were trivial, and I just had not gotten around to them yet. For example, I keep saying that you can write an Apache access control thingy that would restrict access based on the phase of the moon. So last week I wrote one.

So, anyways, here is the mod_perl thing that creates magic user alises for users so that http://foo.com/user/ goes to that user's home directory. It was depressingly easy.

<Perl>
# Folks you don't want to have this privelege
my %forbid = map { $_ => 1 } qw(root postgres bob);

opendir H, '/home/';
my @dir = readdir(H);
closedir H;

foreach my $u (@dir) {
    next if $u =~ m/^\./;
    next if $forbid{$u};

    if (-e "/home/$u/public_html") {
        push @Alias, "/$u/", "/home/$u/public_html/";
    }
}
</Perl>

Yeah, that's all there is to it. That goes in your httpd.conf configuration file, and, of course, will only work if you have mod_perl installed.

Posted by rbowen at November 28, 2002 11:09 PM
Comments
Post a comment