Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

My problem is simply that I have a domain, let's say example.com, and my server's ip address is e.g.

I want to set up 2 different virtual hosts, so when user enters ip address ( in his browser, he will see content from here: /var/www/staging

But if user will type example.com, he will see content from here: /var/www

I think it's possible but I was playing around with it and couldn't make it work.

Also I don't want to make simple redirection. I know I can, but I need both of my apps (live & staging) working in root on the same server. I can't buy second domain, and I can't associate new ip address.

share|improve this question

closed as not constructive by EEAA, John Gardeniers, Scott Pack, mdpc, Sven Oct 29 '12 at 23:46

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

Why -1? if it's impossible then let me know. If I'm missing any info - let me know as well... I will provide. –  Karol Oct 29 '12 at 4:43
Why can't you buy another domain name? –  John Gardeniers Oct 29 '12 at 8:49

2 Answers 2

up vote 2 down vote accepted

It's certainly possible.

With name-based virtual hosts, requests without a Host: header or with the Host: header set to an IP address will go to the default virtual host.

Requests with the Host: header set to example.com will go to the first virtual host listed that matches that name in either the ServerName or ServerAlias directive.

You can use apache2ctl -S to see what virtual hosts you currently have configured and which one is the default.

share|improve this answer
I made it yesterday in exactly this way, and it works. I thought I will post my working solution today, but you were first. Thanks! –  Karol Oct 29 '12 at 23:14

It would be very much easier if you just used Name Based Virtual Hosting. Configure your DNS with A records pointing example.com and staging.example.com to the machine's IP address and then have two vhosts suitably configured waiting to respond

Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com

<VirtualHost *:80>
    ServerName staging.example.com
    DocumentRoot /var/www/staging.example.com
share|improve this answer
Why can't I mark two questions as accepted... –  Karol Oct 29 '12 at 23:15

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