How do I redirect subdomains to a different port on the same server?

I have some subdomains I want to redirect to specific ports on the same server. Say I have

dev.mydomain.com 

I want dev.mydomain.com to transparently redirect to mydomain.com:8080 and I want to preserve the original sub-domain name the url of the browser.

How do I do this with Apache 2.2? I have Apache 2.2 running on default port 80. I can't figure out the write configuration to get this to happen.

I have already set up dev.mydomain.com to resolve in DNS to mydomain.com.

This is for an intranet development server that has a non-routable ip address so I am not so concerned about exploits and security that would compromise a publicly facing server.


Solution

Here is what I finally came up with after being set in the right direction by Miles Erickson. I wanted the address bar to reflect the original subdomain/domain of the request and not the redirected server and port, but he put me on the right path to Google up a solution using VirtualHost and I finally found a solution that included the use of mod_proxy.

First, make sure mod_proxy is enabled:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo systemctl restart apache2

Next, add the following to your site config (e.g., /etc/apache2/sites-available/000-default.conf):

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName dev.mydomain.com
    ProxyPreserveHost On

    # setup the proxy
    <Proxy *>
        Order allow,deny
        Allow from all
    </Proxy>
    ProxyPass / http://localhost:8888/
    ProxyPassReverse / http://localhost:8888/
</VirtualHost>

Run the following line on terminal (specify your domain and sub domain name correctly)

sudo nano /etc/apache2/sites-available/subdomain.example.com.conf 

Paste the following code and change as your requirement

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName subdomain.example.com
        ServerAlias subdomain.example.com
        ProxyRequests Off

        #ProxyPass / http://localhost:8080/
        <Location />
                ProxyPreserveHost On
                ProxyPass http://example.com:8080/
                ProxyPassReverse http://example.com:8080/
        </Location>
     # Uncomment the line below if your site uses SSL.
     #SSLProxyEngine On
</VirtualHost>

Run the following lines on terminal (specify your domain and sub domain name correctly)

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2ensite subdomain.example.com.conf
sudo service apache2 restart

Assuming that dev.mydomain.com can be resolved to mydomain.com's IP, you could add the following to your httpd.conf:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerName dev.mydomain.com
    redirect / http://mydomain.com:8080/
</VirtualHost>

Relevant Apache documentation:

  1. Guide to creating name-based virtual hosts
  2. Core, including VirtualHost and NameVirtualHost
  3. Redirect

Related question: Apache redirect based on hostname

(Note: the original version of this answer incorrectly suggested the use of RedirectMatch, which, as @ChrisS helpfully pointed out, cannot parse the domain portion of the URL.)