Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have set up port forwarding to point to my works webserver so that selected users can access a site via our external IP address when off site. The forwarding to the server works, but on this server there are several Configs, and at the moment it is just stopping at the first one, so the wrong website is shown.

For example, take these two configs, and our external IP address is - what I would like to be able to do is go to (random port, can be any) and have the website in Config 1 show. However, the website in Config 2 is showing, because it comes first alphabetically in the list of configs. Is there a way (similar to the ServerAlias) that I can check which port has been accessed and use the corrisponding config?

Config 1

Listen *:789
NameVirtualHost *:80
NameVirtualHost *:789

<VirtualHost *:80 *.789>
        DocumentRoot /var/www/html/website/test
        ErrorLog /var/log/httpd/
        CustomLog /var/log/httpd/ combined
        ErrorDocument 401 /wp-content/themes/dyne_drewett/401.php

Config 2

<VirtualHost *:80>
        ServerAlias dummy
        DocumentRoot /var/www/html/intranet/intranet_test
        ErrorLog /var/log/httpd/
        CustomLog /var/log/httpd/ combined
        ErrorDocument 403 /wp-content/themes/dyne_drewnett/403.php

Is there away I can amend the configs so that the correct website is shown when doing this? Thanks.

share|improve this question
What webserver are you talking about? IIS, Apache? – Paulo Scardine Jul 31 '12 at 10:35
Apache. Thanks. – David Gard Jul 31 '12 at 10:54
@PauloScardine - i suspect he's talking about Apache 2, given that the post is tagged as such :) – Oliver G Jul 31 '12 at 11:05
Oh dear, I found why this was not working as I wished. What I have above is actually correct, but I had accidently forwarded to the wrong port, so that is why it was using the first config, as it didn't know which one it should actually use! Thanks. – David Gard Jul 31 '12 at 11:36
up vote 2 down vote accepted

When you have several domain names hosted at the same IP address, the webserver uses the domain present at the request headers to figure out what content is to be served. Most apache instalations came with a "VirtuaHost" configuration by default even if it is serving only one site.

You can make one site the default when the request does not have enough information to pick the right VirtualHost, check the Apache documentation about a default virtual host configuration.

The other way is to point some domain name to the external IP and include this domain name in the VirtualHost config using a ServerAlias directive.


You can't serve more than one website on the same ip/port using name-based vhosts, but you can serve them at alternative TCP ports (or IP adresses, if you have more than one available).

If you can't set up an external DNS, in order to use name-based vhosts you can resort to this trick: edit the client machine /etc/hosts (or %WINDIR%\system32\drivers\etc\host) file to point the same internal dns name to your external IP.

share|improve this answer
Thanks for the reply. I am using Virtual Hosts (about 7 of them), and I use the ServerAlias directive successfully internally, where our DNS includes entries for these website. What I'm doing is connecting to the webserver via port forwarding thourgh our external IP, so there is no ServerAlias to lookup. – David Gard Jul 31 '12 at 11:13
@DavidGard: what prevents you from mapping the external IP to a domain name like '' and include another ServerAlias to the desired vhost? – Paulo Scardine Jul 31 '12 at 11:16
If I add an entry to the internal DNS then you would need to be on site to use it. And I can't add am external DNS entry as I've been asked not to by the powers that be. – David Gard Jul 31 '12 at 11:25
@DavidGard: so you need to setup a _default_ vhost or make the desired vhost the default by moving it to the first vhost configuration, please refer to the link in the answer. – Paulo Scardine Jul 31 '12 at 11:29
I can see that in the short term that is probably the answer, but in the future if I need to do this for another of our internal websites then I'd hit the same problem. – David Gard Jul 31 '12 at 11:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .