Redirecting webmail.* requests in a Plesk

Translation of the original in Catalan. Read it in: CatalàCastellano

The goal is to get all requests of the style: http://webmail.domini.tld to be redirected to: http://domini.tld/webmail on a server with Plesk.

We create the apache configuration file: /etc/httpd/conf.d/webmail-redir.conf

<VirtualHost ip_publica_1:80 ip_publica_2:80 ... ip_publica_3:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^webmail.* [NC]
RewriteMap  hostname prg:/opt/scripts/hostname.sh
RewriteRule ^(.*) http://${hostname:%{HTTP_HOST}}/webmail [L,R]
</VirtualHost>

and we create the script: /opt/scripts/hostname.sh (perms: 755)

#!/bin/sh

while read inputline
do
        domini=$(echo $inputline| cut -d "." -f 2)
        tld=$(echo $inputline|cut -d "." -f 3)
        echo $domini.$tld
done

In essence what we are doing is taking advantage of apache2's mod_rewrite to be able to assign to the map function a script that trims the HTTP_HOST by removing the word webmail from the beginning. With that we get the domain we want to connect to and we only have to add the URI to the RewriteRule so that it redirects us where it should.