4

I am rebuilding an old server. Only one of the five old sites need to go live and all five of the old sites point to the same IP address.

Unfortunately any time a request is made for one of the old sites via their own unique domain names they are passed the pages from the one site that I'm trying to make live. I would just like the old sites to display a 404 instead of the one site that I'm actually trying to make live.

As an example:

  • Site 1 (192.168.0.1) [www.first.com]
  • Site 2 (192.168.0.1) [www.second.com]
  • Site 3 (192.168.0.1) [www.third.com]

I want to move site 1 live again. So I turned on apache on the machine with the IP address of 192.168.0.1, with all of site 1's files in the /var/www directory. When I browse to 192.168.0.1 or www.first.com I correctly see site 1. When I browse to www.second.com and www.third.com I also receive site 1 which is the problem.

Is there a way I can 404 any request which comes in without www.first.com?

Here is my sites-available:

NameVirtualHost *:80

LogFormat "%h %l %u %t \"%r\" %>s %b" common

<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</Files>

<Directory />
  Order Deny,Allow
  Deny from all
  Options None
  AllowOverride None
  Options None
</Directory>

<Directory /var/www>
  Order Allow,Deny
  Allow from all
</Directory>

<VirtualHost *:80>
        ServerName first.com
        ServerAlias www.first.com
        DocumentRoot /var/www
        ErrorLog /var/log/first.com.err
        CustomLog /var/log/first.com.log common
</VirtualHost>

<VirtualHost _default_:*>
        ServerName unknown
        DocumentRoot /var/www_404
</VirtualHost>

2 Answers 2

4

A request with no matching ServerName or ServerAlias will be served by the first block to load; just move your 404'ing VirtualHost to the top (its bindings should match what it's playing default for, so set it to *:80 - the wildcarded vhost won't get any requests without that.)

0
1

For the default VirtualHost, try the following (at the top, not the bottom as you have it):

<VirtualHost _default_:*>
    DocumentRoot /var/www_404
</VirtualHost>

Otherwise, it's going to try to search for a server named "Unknown" if you have it at the bottom ;-)

Alternatively, you can just specify the DocumentRoot outside of your VirtualHost directives to define a default location, as specified in the Apache documentation here

EDIT: Minor correction about the placement

3
  • From the VirtualHost documentation: "A default vhost never serves a request that was sent to an address/port that is used for name-based vhosts. If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file)." Server name of "Unknown" is OK, as long as it's the first name-based vhost to load it'll get unmatched requests. Mar 18, 2011 at 16:55
  • True. And the alternative I noted at the bottom will also work. I've revised the answer to clarify it.
    – Hyppy
    Mar 18, 2011 at 17:02
  • 1
    The tricky part with the first option is that the _default_:* port specification will be completely ignored by Apache except for ports other than 80, since there are vhosts set up on that port. From that doc page, this port specification only catches "an address/port combination that is not used for any other virtual host." Mar 18, 2011 at 17:15

You must log in to answer this question.

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