• 4
Votes
name
name Punditsdkoslkdosdkoskdo

How do I redirect subdomain to 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:8080and 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.

 

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 admin@example.com
        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
  • 0
Reply Report

Just to level set--

  1. You have two ASP.NET applications running on the same machine. One is intended to serve content for www.yourdomain.com and the other is meant to serve content for test.yourdomain.com

  2. The two ASP.NET application, obviously, live in different folders, and have different IIS application names. You didn't say what they were, but for the sake of example I shall assume the application names are TestApplication and ProductionApplication, with matching virtual directory names.

  3. You want all traffic to go to the one box. If the host that the user typed into the URL is www.yourdomain.com, the request should be handled by ProductionApplication; otherwise it is handled by TestApplication.

So in terms of routing...

Am I understanding the requirements?

If so, it's easy to do with an IIS redirect rule, which you can set in web.config.

<rewrite>
    <rules>
        <rule name="AnyName" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^test.yourdomain.com$" />
                <add input="{PATH_INFO}" pattern="^/TestApplication/" negate="true" />
            </conditions>
            <action type="Rewrite" url="TestApplication{R:0}" />
        </rule>
    </rules>
</rewrite>

This tells IIS that whenever it receives a request for test.yourdomain.com, it is to modify the URL and insert "TestApplication" before the path that was provided.

For production you'd do something very similar:

<rewrite>
    <rules>
        <rule name="AnyName" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^www.yourdomain.com$" />
                <add input="{PATH_INFO}" pattern="^/ProductionApplication/" negate="true" />
            </conditions>
            <action type="Rewrite" url="ProductionApplication{R:0}" />
        </rule>
    </rules>
</rewrite>

Detailed instructions can be found on Scott Forsyth's Blog.

  • 0
Reply Report

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.

<VirtualHost *:80>
    ServerAdmin me@mydomain.com
    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>
  • 0
Reply Report