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?


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



Machine is debian w/ Apache2


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

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

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


  • 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.
    – user30665
    Jan 4 '10 at 5: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. Jan 4 '10 at 5: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,
    – user30665
    Jan 4 '10 at 5:21

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

  • Not for apache 2.2. Any ideas how i can do it for apache 2.2? Thanks
    – jrosell
    Jun 5 '15 at 11:18

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.