Redirect subdomain virtual host subdomains to path

apache-2.2mod-rewriteredirectsubdomain

I used Apache's virtual host for a ton of subdomains for greptweet.com, where each twitter username is a subdomain, like <username>.greptweet.com. Problem is … subdomains can't have underscores! 🙂

Now I've restructured the site, I want to permanently redirect all users that are going to the subdomain to the path:

<username>.greptweet.com to the path structure greptweet.com/u/<username>

e.g. http://kaihendry.greptweet.com/ to http://greptweet.com/u/kaihendry/

Ideally I'd like to achieve this nearly in the Apache httpd.conf <VirtualHost> stanza instead of writing bits into /srv/www/

Best Answer

Something like this would work:

<VirtualHost *:80>
    ServerName kaihendry.greptweet.com
    ServerAlias *.greptweet.com
    DocumentRoot /var/www/localhost/htdocs/
    Include /etc/apache2/vhosts.d/default_vhost.include

    <Directory "/var/www/localhost/htdocs">
        Order allow,deny
        Allow from all
    </Directory>

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^([^.]+)\.greptweet\.com [NC]
    RewriteRule ^(.*)$ http://greptweet.com/u/%1 [R,L]

</VirtualHost>
Related Topic