4

I have multiple hostnames resolving to a single machine, e.g.:

  • build.mydomain.com
  • www.mydomain.com
  • jira.mydomain.com
  • mydomain.com

Is it possible to setup apache in order to redirect requests to each different hostname?

e.g:

  • build.mydomain.com -> build.mydomain.com:8111,
  • www.mydomain.com -> www.mydomain.com:8080
  • mydomain.com -> www.mydomain.com:8080

The DNS records are setup to all point to the same machine, i just want to redirect to the right port given the hostname.

Cheers,

Edit:

Machine is debian w/ Apache2

Edit2:

<VirtualHost *:80>
    ServerName www.mydomain.com
    ServerAlias mydomain.com
    redirect 301 / http://www.mydomain.com:8080/
</VirtualHost>

<VirtualHost *:80>
    ServerName build.mydomain.com
    redirect 301 / http://build.mydomain.com:8111/
</VirtualHost>

3 Answers3

3

Follow the documentation about canonical hostnames which lead to <If> directive :

<If "%{HTTP_HOST} == 'build.mydomain.com' && %{SERVER_PORT} -eq 80">
    RedirectMatch permanent /.* http://build.mydomain.com:8111
</If>

<If "%{HTTP_HOST} == 'www.mydomain.com' && %{SERVER_PORT} -eq 80">
    RedirectMatch permanent /.* http://www.mydomain.com:8080
</If>

...
Anthony O.
  • 674
  • 1
  • 5
  • 13
2

Actually, I doubt using redirect is winning strategy for what you're trying to do. I'd recommend using mod_proxy to create a reverse proxy which will hide the way you've built your system.

And if I had the choice, I would use something more lightweight and more convenient to configure, like Perlbal.

af.
  • 999
  • 1
  • 8
  • 4
1

Are you running multiple instances of Apache, each listening on a different port? Why? Virtual hosts will take care of everything.

http://httpd.apache.org/docs/2.2/vhosts/name-based.html

Max Alginin
  • 3,284
  • 14
  • 11
  • No, single instance of apache that I wanted to use to redirect requests. Got multiple instances of tomcat running, e.g. TeamCity on 8111, tomcat on 8080 and JIRA on 8081. I wanted to redirect to the respective port given the hostname. –  Jan 04 '10 at 05:10
  • how about mod-rewrite running on the apache:80 instance? possibly even apache as proxy but that sounds like far too much effort for a redirect. – BuildTheRobots Jan 04 '10 at 05:18
  • I have it half working, using VirtualHost directive with a redirect 301 to the same hostname and different port. This should do for now. Cheers, –  Jan 04 '10 at 05:21