Is it possible to configure Apache for different virtual hosts based on the source IP? (i.e. same interface, same hostname, but two different virtual hosts, with different content, based on source IP.)
The motivation for this is so that my IP address can access the site proper, but that everyone else gets the holding page. The conventional solution seems to be to use mod_rewrite to direct visitors to a separate page within the same docroot, but I'd like to use a completely different docroot for the holding page instead.
It wouldn't really be a different virtual host. But using something like mod_rewrite or mod_alias you can serve content out of any folder for which you have set the appropriate permissions. There's only one docroot, but you can effectively change that on the fly.
One way to do it might be:
Do note though that it'd probably be a bit cleaner to handle this with a dev subdomain.
I don't know if that's possible (without mod_rewrite, anyway) in Apache level.
Here's another idea. What if you set up two Apache virtual hosts and then use iptables to transparently forward visitor to correct virtual host? Something like
Or something similar. :)
Apache 2.3 or later
With Apache 2.3 or later you can apparently do something like this (tested):
Apache 2.2 or earlier
Update: This is not a good solution. See below.
You have to do a hack like this. Note the
[PT]
which stands for "passthrough". Without it, an actual HTTP redirect is sent back to the client, which probably isn't what you want. The[OR]
thing (which stands for "or") shows how to match multiple addresses.You need to enable
mod_rewrite
which you can do on Debian/Ubuntu with this command:Note that this method doesn't completely ban other people from accessing your test site, so you will probably want to add some security, or just choose a more obscure prefix than
next
.Update on the mod_rewrite method.
There are a couple of problems with this method. First, Django does not work with two sites in the same process like this, you need to follow the instructions in this answer.
Secondly mod_rewrite does not work with
POST
requests! AllPOST
s are silently changed toGET
and the post data is discarded. Very frustrating! Therefore I recommend you use the...iptables version
Simply run the servers on two different ports. This one includes the WSGI stuff to have two separate django sites.
Then you can use this
iptables
command to route requests from your ip address on port 80 to port 1222:Change
-A
to-D
to remove the rule.Note that the docs suggest you need to add additional
Listen
andNameVirtualHost
commands, but I actually found that it works without them, and adding them made it break (at least in ubuntu).AFAIK, the only way to do this is to symlink a location within the document root to your content outside the document root, then rewrite the request to that.
Like @Timmmm said, but correcting the ipmatch statement (note the '10.10.10.10'): ServerName www.example.com
Because otherwise it will show the error: