Redirecting Webmail Path

I’d like to share a handy tip for those migrating from VestaCP to HestiaCP.

In VestaCP, the path to Webmail was:

https://www.mydomain.com/webmail

While in HestiaCP, it is:

https://webmail.mydomain.com

To avoid the inconvenience of informing all your clients about the URL change, you can resolve this issue with a simple change in your .htaccess file.

Just add the following code to your .htaccess file and your clients will be automatically redirected."

RewriteCond %{REQUEST_URI} ^/webmail(/|$|\?)
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^(.*)$ https://webmail.%1/ [L,R=301]
1 Like

Thanks for sharing.

You can do it too adding a redirect in Nginx and it will be valid for Nginx Standalone and Nginx+Apache2.

Example:

Create two files:
/home/USER/conf/web/DOMAIN/nginx.conf_redirect-webmail
And
/home/USER/conf/web/DOMAIN/nginx.ssl.conf_redirect-webmail (this one could be a symlink to the previous one)

With this content:

location /webmail {
      return 301 $scheme://webmail.HereTheDomain;
}

Script to modify at once all users that have a mail domain and a web domain.

#!/usr/bin/env bash
for i in $(v-list-users plain | cut -f1); do
        mail_dom="$(v-list-mail-domains "$i" plain | cut -f1)"
        if [[ -n "$mail_dom" ]]; then
                for f in $mail_dom; do
                        web_conf="/home/$i/conf/web/$f"
                        if [[ -d "$web_conf" ]]; then
                                echo "Redirecting webmail for user $i and domain $f"
                                redirect_file="/home/$i/conf/web/$f/nginx.conf_redirect-webmail"
                                redirect_ssl_file="/home/$i/conf/web/$f/nginx.ssl.conf_redirect-webmail"
                                cat <<EOF >"$redirect_file"
location /webmail {
      return 301 \$scheme://webmail.$f;
}
EOF
                                ln -sfr "$redirect_file" "$redirect_ssl_file"
                        else
                                echo "User $i doesn't have web domain for $f so no redirect applied"
                        fi
                done
        else
                echo "User $i doesn't have mail domains"
                continue
        fi
done
echo
if nginx -t &>/dev/null; then
        echo -n "Reloading nginx... "
        if systemctl reload nginx; then
                echo "OK"
        else
                echo "ERROR"
        fi
else
        echo "ERROR: nginx -t produced an error, review nginx conf" >&2
        nginx -t
fi
1 Like

Very good, Sahsanu.

Beautiful solution :slight_smile:

1 Like