Server-wide redirect of domain.tld/webmail to webmail.domain.tld


We need to set up a server-wide redirect rule in httpd/conf.d but I'm not sure of the exact syntax. There are hundreds of domains, a mixture of .com,, .net etc.

Basically, anybody accessing domain.tld/webmail should be redirected to webmail.domain.tld.


The reason for this is a change of hosting provider. The old host supplied email via /webmail, whereas the new host uses

A 301 redirect would be ideal, but it must be server-wide, not htaccess.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David S.Commented:
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^(www\.)?([a-zA-Z0-9.-]+)$
RewriteRule ^/webmail    http://webmail.%2/ [L,R=301]

Open in new window

redcarrotAuthor Commented:
I have tried the above suggestion, but it didn't work.

Copy the above code in and save.
Restart Apache.

The code is having no effect.
Steve BinkCommented:
Turn on your rewriting log and post the results of a single attempt.
redcarrotAuthor Commented:
Problem solved :

1) Create a new file: /etc/httpd/conf.d

Alias /webmail /var/www/html/webmail

<Directory "/var/www/html/webmail/">
AllowOverride All

2) Create another new file: /var/www/html/webmail/index.php

<?php header("Location:http://webmail." . str_replace("www.", "", $_SERVER['HTTP_HOST'])); ?>

3) That's it. Restart Apache, and now redirects to

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
redcarrotAuthor Commented:

1) Create a new file: /etc/httpd/conf.d

should read

1) Create a new file: /etc/httpd/conf.d/webmail.conf

How do I edit my post?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.