Linux – HowTo rewrite subdomain to apache userdir

apache-2.2linuxmod-rewritemod-userdir

As described in this blog I want to rewrite internally

http://xy.user.phpfarm.lan -> http://user.phpfarm.lan/~xy/

Module userdir is enabled.

With the following Apache2 vhost config file, it has got a bug:

/etc/apache2/sites-enabled/phpfarm:

<Virtualhost *:80>
 ServerAdmin root@localhost
 ServerName em-sv-phpfarm.lan
 ServerAlias *.user.phpfarm.lan user.phpfarm.lan phpfarm.lan

 DocumentRoot /var/www/
 <Directory />
  Options FollowSymLinks
  AllowOverride None
 </Directory>
 <Directory /var/www>
  Options Indexes FollowSymLinks MultiViews
  AllowOverride None
  Order allow,deny
  allow from all
 </Directory>

 ErrorLog ${APACHE_LOG_DIR}/error.log

 # Possible values include: debug, info, notice, warn, error, crit,
 # alert, emerg.
 LogLevel warn

 CustomLog ${APACHE_LOG_DIR}/access.log combined

 RewriteLog "/tmp/rewrites"
 RewriteLogLevel 5
 # Uncomment the above two lines and watch /tmp for some debug info.

 RewriteEngine On

 # Make bob.user.phpfarm.em.lan be treated as user.phpfarm.lan/~bob
 RewriteCond %{HTTP_HOST} ([^.]+)\.user\.phpfarm\.lan$ [NC]
 # In the above line make sure you escape (preceed with a backslash) every period (.) in the address.
 RewriteCond %{REQUEST_URI} !^[^/]+/~.*$
 # exclude file handler
 RewriteCond %{REQUEST_URI} !^/php-fcgi/.*$
 RewriteRule ^/([^~]+) /~%1/$1 [PT,L]
</virtualhost>

So I think an empty REQUEST_URI isn't captured correctly. But I can't find the bug. Does anybody know, why?

Best Answer

Try replacing the last line with

 RewriteRule ^/([^~]*) /~%1/$1 [PT,L]

You were insisting on at least one character after the slash, so cases where REQUEST_URI was / were being ignored.