Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Dynamically rewrite mod userdir URLs (site.com/~user) to user.site.com

Dynamically rewrite mod_userdir URLs (site.com/~user) to user.site.com

4:48 pm on Feb 25, 2013 (gmt 0)

New User

joined:Feb 25, 2013
posts: 2
votes: 0

I run a small web host with automatic installation of Wordpress at signup. When a client signs up, Wordpress is automatically installed using a script running after CPanel has created the user.

All the customers get their own .com/.net/.org domain at signup, however since domain propagation takes some time (and I advertise that their sites are installed within 60 seconds after signup), I need to provide a temporary URL in the meantime.

I'm using the Apache module mod_userdir for that, and it works okay, except for one thing. The resulting URLs are formatted like this: StagingDomain.com/~username. The tilde sign ( ~ ) is the problem.

It confuses both the customers (who often don't know how to type the ~ sign) and Wordpress. In order for Wordpress to work properly I have to resort to some stupid hacks/workarounds that must be uninstalled when the domain propagation is done and the site is ready for the domain.

It would be great to be able to provide the customers with their own subdomain instead.

The right way to do this would, as far as I have understood, be to use mod_vhost_alias [httpd.apache.org] to map virtual subdomains to the mod_userdir folders.

However, I simply can't wrap my head around how to do it. I've tried a lot of things, but there are simply too many different factors in play that I don't understand sufficiently to get it to work.

I hope you guys and girls can help me with that.

Current setup

Distro: CentOS 6.3 (fully updated)
Apache: 2.4.3
PHP: 5.4.11 (with SuPHP)

CustomerDomain.com - the domain the customer registered at signup and that will be used after domain has propagated
StagingDomain.com - a domain I own. I want the Linux username of the customer, currently used in the "~username", to be a subdomain to the domain

The files are loaded/executed from /home/customeruser/public_html/

Virtualhost for CustomerDomain.com:
<VirtualHost serverip:80>
ServerName customerdomain.com
ServerAlias www.customerdomain.com
DocumentRoot /home/customeruser/public_html
ServerAdmin webmaster@customerdomain.com
UseCanonicalName Off
Options -ExecCGI -Includes
RemoveHandler cgi-script .cgi .pl .plx .ppl .perl
CustomLog /usr/local/apache/domlogs/customerdomain.com combined
CustomLog /usr/local/apache/domlogs/customerdomain.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."

UserDir disabled
UserDir enabled customeruser

<IfModule mod_suphp.c>
suPHP_UserGroup customeruser customeruser
<IfModule !mod_disable_suexec.c>
<IfModule !mod_ruid2.c>
SuexecUserGroup customeruser customeruser
<IfModule mod_ruid2.c>
RUidGid customeruser customeruser

Virtualhost for StagingDomain.com
<VirtualHost serverip:80>
ServerName stagingdomain.com
ServerAlias www.stagingdomain.com
DocumentRoot /home/staginguser/public_html
ServerAdmin webmaster@stagingdomain.com
UseCanonicalName Off
CustomLog /usr/local/apache/domlogs/stagingdomain.com combined
CustomLog /usr/local/apache/domlogs/stagingdomain.com-bytes_log "%{%s}t %I .\n%{%s}t %O ."

UserDir enabled

<IfModule mod_suphp.c>
suPHP_UserGroup staginguser staginguser
<IfModule !mod_disable_suexec.c>
<IfModule !mod_ruid2.c>
SuexecUserGroup staginguser staginguser
<IfModule mod_ruid2.c>
RUidGid staginguser staginguser
ScriptAlias /cgi-bin/ /home/staginguser/public_html/cgi-bin/


The resulting temporary (mod_userdir) url for the two Virtualhosts above is stagingdomain.com/~customeruser. It works, but it is messy, cumbersome, and it looks really unprofessional.

customeruser.stagingdomain.com would be a much better option as a temporary url for my clients. I know it's possible, I just can't figure out how to do it.
12:20 am on Feb 26, 2013 (gmt 0)


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 44

welcome to WebmasterWorld, kristofferR!

you could configure your DNS and web server for wildcard subdomains and use mod_rewrite directives for doing an external redirect from subdirectories to subdomains and an internal rewrite from subdomains to subdirectories.

however the customer subdomains on your staging domain should probably be protected by basic HTTP authentication to avoid public access to staged content and non-canonical url-related issues.
12:36 pm on Mar 5, 2013 (gmt 0)

New User

joined:Feb 25, 2013
votes: 0

Thanks! How would this work with suPHP? When I tryed using mod_rewrite I just got a lot of suPHP releated errors.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members