# Solved: How to redirect a subdomain to a port in Apache?



## Soundy (Feb 17, 2006)

Quite simply: I'm running mail and webserver on the same system... the mailserver has its own webmail interface on port xxxx. I want, for example, 'http://webmail.mydomain.com' to redirect to "http://mydomain.com:xxxx". How would I do this in Apache 2.2?


----------



## ehymel (Aug 12, 2007)

Soundy said:


> Quite simply: I'm running mail and webserver on the same system... the mailserver has its own webmail interface on port xxxx. I want, for example, 'http://webmail.mydomain.com' to redirect to "http://mydomain.com:xxxx". How would I do this in Apache 2.2?


In the apache config file (or within one of the conf.d/*.conf files, use the Redirect command.


```
Redirect permanent / http://mydomain.com:xxxx
```
The 'permanent' designation is optional.

Another good solution if you want *ALL* requests for a page within the the default port 80 request to be redirected to the same request within another port, something like this could be used:


```
RedirectMatch permanent /(.*) http://mydomain.com:xxxx/$1
```
Hope this helps.


----------



## Soundy (Feb 17, 2006)

Hmmm that's redirecting EVERYTHING to port xxxx - I don't want mydomain.com or www.mydomain.com to redirect, just webmail.mydomain.com


----------



## ehymel (Aug 12, 2007)

I guess I should have been more specific.

The Redirect statements should go within your <VirtualHost> section for the subdomain you are wanting to redirect.

For example, I use the following section to redirect all port 80 requests to a SSL connection on 2 different web-based email domains that I maintain, along with a snippet to show how I use a permanent redirect. Hopefully this will show some examples of how I use these:


```
<VirtualHost *:80>
    ServerName mail.e-medico.com
    ServerAlias group.e-medico.com
    RedirectPermanent / https://group.e-medico.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName mail.setexonc.com
    RedirectPermanent / https://mail.setexonc.com/
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /var/www/sites.clients/setexonc.com/www
    ServerName setexonc.com
    ServerAlias www.setexonc.com

    RedirectMatch /treatment/(.*).htm$ http://www.rtanswers.org/treatment/$1.htm
    RedirectMatch /index.html /
    RedirectPermanent /mail https://mail.setexonc.com/
    ErrorDocument 404 /error.php
    <Directory /var/www/sites.clients/setexonc.com/>
        allow from all
        order allow,deny
    </Directory>
</VirtualHost>
```


----------



## Soundy (Feb 17, 2006)

Oooooh I see, got it, thanks.

Works perfectly now!


----------

