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

Hi

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, .co.uk, .net etc.

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

Examples:

http://www.domain1.com/webmail
-> http://webmail.domain1.com

http://domain1.com/webmail
-> http://webmail.domain1.com

http://www.domain2.co.uk/webmail
-> http://webmail.domain2.co.uk

http://domain2.co.uk/webmail
-> http://webmail.domain2.co.uk

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

A 301 redirect would be ideal, but it must be server-wide, not htaccess.
redcarrotAsked:
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

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

/etc/httpd/conf.d/webmail.conf
Copy the above code in and save.
Restart Apache.

The code is having no effect.
0
Steve BinkCommented:
Turn on your rewriting log and post the results of a single attempt.
0
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
</Directory>

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 anydomain.com/webmail redirects to webmail.anydomain.com.
0

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:
Oops...

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?
0
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.