redirect a specific subdomain to domain.com:port

Operating System & Version
CentOS 7.9.2009
cPanel & WHM Version
cPanel & WHM 98.0 (build 6)

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
Hello
I was using CWP before moving to cPanel
and in CWP we used mail.domain.com URL to access webmail(Roundcube)
and there's an A record for mail pointed to domain.com in DNS records
and in phone/desktop mail clients we used mail.domain.com in the incoming and outgoing server

So when I moved to cPanel,
mail.domain.com is still an incoming and outgoing server
but mail.domain.com URL doesn't access webmail(Roundcube), it opens the same page of domain.com

Since i want mail.domain.com to opens webmail.domain.com

I've added a CNAME record that makes mail.domain.com alias to webmail.domain.com
But still it opens the main domain page not webmail.domain.com


and we don't need that, because all employees are saved mail.domain.com URL in bookmarks

---

after searching cPanel forums I've found many topics about activating Service subdomains in the tweaks section in WHM, and it's already activated but i don't know how to change or add mail.domain.com as an alias to webmail.domain.com


and if I made a subdomain named: mail.domain.com, and made a redirect to domain.com:2096, it works for all users saved mail.domain.com as a bookmark
but the incoming and outgoing server changes to: domain.com instead of mail.domain.com - then all desktop/phone mail clients is not working anymore, SO creating a subdomain named: mail, isn't fixing the issue.

So, how to make webmail opens using mail.domain.com without creating subdomain and keeping incoming/outgoing server the same as: mail.domain.com


Please simplify the answer because I'm new to cPanel and Linux world, we are only hosting a small website with emails, Thanks
---

Hint: cp.domain.com is the hostname i set
 
Last edited:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
Hey there! By default, cPanel creates the following two records in the DNS for the domain:

mail IN CNAME domain.com.
webmail IN A x.x.x.x

where "x.x.x.x" is the IP address of your server.

In addition to that work, we included "mail.domain.com" as a ServerAlias entry in Apache. This does two things:

-allows the SSL certificate for the domain to include the mail subdomain, allowing mail clients to connect securely
-does cause someone browsing to mail.domain.com to reach the main domain, although that usually doesn't happen as most users wouldn't manually type that in.

With the way this is configured in cPanel, there isn't a way to make those changes. It is part of both the cPanel software templates and the Apache templates. The best thing to do would be to explain to your users that you've changed systems and that they should now use the "webmail" subdomain to access email.
 

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
Hey there! By default, cPanel creates the following two records in the DNS for the domain:

mail IN CNAME domain.com.
webmail IN A x.x.x.x

where "x.x.x.x" is the IP address of your server.

In addition to that work, we included "mail.domain.com" as a ServerAlias entry in Apache. This does two things:

-allows the SSL certificate for the domain to include the mail subdomain, allowing mail clients to connect securely
-does cause someone browsing to mail.domain.com to reach the main domain, although that usually doesn't happen as most users wouldn't manually type that in.

With the way this is configured in cPanel, there isn't a way to make those changes. It is part of both the cPanel software templates and the Apache templates. The best thing to do would be to explain to your users that you've changed systems and that they should now use the "webmail" subdomain to access email.
Hello cPRex,
Thanks for your reply

I have the DNS records sets correctly as you mentioned
and regarding:

In addition to that work, we included "mail.domain.com" as a ServerAlias entry in Apache. This does two things:
I don't know how to do that exactly

and yeah we do want anyone to access the URL: mail.domain.com to open the webmail.domain.com or domain.com:2096


maybe change default webmail.domain.com to be mail.domain.com as a main webmail login URL
or
adding mail.domain.com to be an alias of webmail.domain.com
or
make mail.domain.com redirect to webmail.domain.com somehow, without creating mail subdomain


So how to make it happen using any of these suggestions without causing any issues?

Thanks and waiting for your reply
 

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
So, there's no way to even change the main webmail URL from the cPanel Configs? for sure this link isn't hardcoded !!
and i'm only talking about 1 account, we do have license for only 1 account.
 

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
so there's no way to add a new service subdomain that acts like webmail ?

or maybe creating a subdomain named mail and redirect it to webmail.domain.com ---< using this idea changes incoming / outgoing server
from mail.domain.com to domain.com

so is there a way to create that subdomain named: mail and use it to redirect to webmail.domain.com without changing incoming / outgoing server? and keeping them as mail.domain.com

because if incoming / outgoing server changed to domain.com the emails in mail clients won't work

---

Another idea


can we use .htaccess in virtual sub-domain or anywhere and do the redirect from webmail.domain.com to mail.domain.com on it?
 
Last edited:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
There isn't a way to change the service subdomain behavior at this time. If you wanted to see options to create custom service subdomains, that would make an excellent feature request. You can open a feature request using the link in my signature.

For the .htaccess option, it wouldn't hurt to try it out, but this isn't something we test on our end.
 

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
There isn't a way to change the service subdomain behavior at this time. If you wanted to see options to create custom service subdomains, that would make an excellent feature request. You can open a feature request using the link in my signature.

For the .htaccess option, it wouldn't hurt to try it out, but this isn't something we test on our end.
That's a good idea to submit a feature request for that since there's a lot of posts requesting the same feature

regarding .htaccess option, i don't know how to do it exactly, so if you can help me to test it at my end ?
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
I did see the feature request come in and got that approved.

Using the following code worked well for me on a test domain when placed at the top of the .htaccess file. You want to make sure it is above the cPanel PHP block or else it may not be read properly:

Code:
RewriteCond %{HTTP_HOST} ^mail.domain.com [NC,OR]
RewriteRule ^(.*)$ https://webmail.domain.com/$1 [L,R=301,NC]
 
  • Like
Reactions: AlvinGenius

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
I did see the feature request come in and got that approved.

Using the following code worked well for me on a test domain when placed at the top of the .htaccess file. You want to make sure it is above the cPanel PHP block or else it may not be read properly:

Code:
RewriteCond %{HTTP_HOST} ^mail.domain.com [NC,OR]
RewriteRule ^(.*)$ https://webmail.domain.com/$1 [L,R=301,NC]
Thanks cPRex
i'm sorry to ask you again, where exactly to put this .htaccess file with the mentioned code you sent.
Like i said i'm new to this stuff.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
7,519
1,028
313
cPanel Access Level
Root Administrator
The .htaccess would be located in the document root for the site, which is /home/username/public_html/.htacces. Just place that code in there, edited to be your actual domain name, and it will likely just work without any other adjustments needed.
 

AlvinGenius

Member
Aug 20, 2020
12
3
3
Egypt
cPanel Access Level
Website Owner
The .htaccess would be located in the document root for the site, which is /home/username/public_html/.htacces. Just place that code in there, edited to be your actual domain name, and it will likely just work without any other adjustments needed.
if i put it into /home/username/public_html/.htacces , then when access mail.domain.com it will redirect it to domain.com, then domain.com will redirect it to webmail.domain.com
I'm i right?