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 redirect to mydomain.com:8080 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 also want to keep the dev.mydomain.com in the URL of the client. I don't anyone to realize it is actually being hosted on a different port, this part is very important.

I have already set up dev.mydomain.com to resolve in DNS to mydomain.com, I want all the request to preserve the subdomain in the address bar of the browser.

This is for an intranet development server so I am not so concerned about exploits and security. This is server has a non-routable ip address as well.

link|improve this question

60% accept rate
do you want to see your client that it is being redirected or do you want it to seem as if everything is running on dev.mydomain.com port 80? – Marcel G Oct 28 '10 at 15:17
everything on port 80 – Jarrod Roberson Feb 17 '11 at 22:33
over 1000 views and only 2 up votes, how disappointing :-( – Jarrod Roberson Mar 1 at 14:33
feedback

4 Answers

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.)

link|improve this answer
see my self provided answer for what I actually used, which is this idea of VirtualHost with mod_proxy to preserve the original host instead of doing a redirect. This is an internal server so I am not concerned with security or exploits of mod_proxy. – Jarrod Roberson Oct 28 '10 at 15:38
feedback

Your looking for mod_rewrite. Here's the link to Apache's documentation which includes many examples for basic and advanced configurations..

And if you're unable to interpret the documentation yourself, try adding this to httpd.conf:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^dev\.example\.com$ [NC]
RrewireRule ^(.*)$ http://example.com:8080$1 [R=301]

And if that's not a clear example, here's a link to a mod_rewrite beginners guide too.

link|improve this answer
who said anything about DNS, I want to configure Apache 2.2 to do this? – Jarrod Roberson Oct 28 '10 at 1:00
2  
I have already read tons of things I found on Google and that documentation, all of which seems to be written for someone who already understands those byzantine options and doesn't need the documentation. There are mod_rewrite, mod_proxy and a host of other options, I don't understand which one is correct and how to use it. – Jarrod Roberson Oct 28 '10 at 1:05
It isn't correct. mod_rewrite should only be used as a last resort, when nothing else will do. The redirect and proxy solutions given are far superior in this (and almost any other conceivable) case. – adaptr Mar 1 at 14:38
@adaptr This does a redirect, I fail to see how any of the other redirects are "far superior". And suggesting that he setup a proxy when he clearly doesn't have a firm grasp of Apache/Security/Performance/etc, is that serious? – Chris S Mar 1 at 15:28
feedback

Add in Ur main vhost config following:

ProxyPreserveHost On
ProxyPass / http://example.com:8080/
ProxyPassReverse / http://example:8080/

But this require mod_proxy on Apache.

link|improve this answer
This is not a redirect; it proxies the connection. Enabling mod_proxy can be extremely easy to exploit if you aren't very careful configuring it. I would highly recommend against this. – Chris S Oct 28 '10 at 12:38
He doesn't want a redirect, as per his OP. He wants the original URL to remain in the address bar; this means using either mod_proxy or mod_rewrite, and y'all better avoid mod_rewrite. – adaptr Mar 1 at 14:40
feedback
up vote 2 down vote accepted

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.

NameVirtualHost *:80

<VirtualHost *>
    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>
link|improve this answer
Looks like a great solution. – Miles Erickson Oct 29 '10 at 20:46
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.