Archive

Archive for April, 2007

Apache UserDir without a tilde

April 24th, 2007
Comments Off

I posted this a while back on an internal blog at my workplace, and a friend recently asked if I knew of a way for this to be done, so I’ll re-post it here. I work for a wholesale ISP, and we often take an existing ISP’s customer base, including email and personal web space hosting, and migrate it to our servers. We’ve used this hack once or twice for some migrated accounts from an ISP, who in the past, allowed users to have personal web space at their domain, but under a “normal” subdirectory of the domain, such as http://example.com/username/. Migrating their subscribers to our servers would have required a change to the Apache-style http://example.com/~username/ format, which would break existing hard links and search engine results.

Our workaround uses Apache’s rewrite module (not surprisingly, named ‘mod_rewrite’).

In the VirtualHost section for the domain I wish to affect, I add the following directives:


UserDir /home/example.com/*/WWW
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond /home/example.com/$1/WWW$2 -f [OR]
RewriteCond /home/example.com/$1/WWW$2 -d
RewriteRule ^/([^/]+)(.*) /home/example.com/$1/WWW$2
</IfModule>

Of course, change the directories to match your local setup.

Now for how it works… Say a web user surfs to http://www.example.com/username/ – The rewrite rule will first check for a file or directory at /home/example.com/username/WWW/’s existence, and if it indeed exists, Apache serves the requested file out of the referenced directory.

A couple of things to be cautious of:

  • This MAY interfere with folder’s in your domain’s website structure! User directories (based on the username) take precedence over your website directories. If a user with the username “images” happens to be added to your system, I can only imagine very bad things may happen to your visitors.
  • I don’t know how much of a strain this is on Apache. From my experience, mod_rewrite is NOT horribly efficient. If at all possible, DON’T DO THIS – just make your users deal with a standard UserDir setup. If they are horribly concerned about the “ugly” tilde in their URL, I’d suggest one of the many options out there for URL forwarding or regular domain hosting.

Random Hacks, Systems Admin