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

On an Apache2 server linked to by a DNS that includes a wildcard entry, e.g. *.example.com, subdomains that are not defined as ServerNames in any VirtualHosts point to the first defined VirtualHost, in my example this is 000-default.

My Question:

How would one get unused subdomains (subdomains not used in any virtualhosts) to return a 404 error to the requesting client? This must preferably show in server logs as a 404 as well.

I have looked into the following possibilities:

  • Redirecting any invalid subdomain to the home page or some other page.

    The problem with this method is, when someone links to your site as this.company.sucks.example.com, the client will see your home page or in my case 000-default if I do not redirect. Thanks, to Mike for pointing this out. (regex for "suck", etc definately not an option)

  • Let the default VirtualHost point to a non-existent directory.

    Apache does not like this one bit, warning with every reload. Beyond the warning, everything seems fine. This seems like a hack. Does this seem like a problem (however small) to anyone?

  • Point the default VirtualHost to a folder where the index.php is forbidden, thus creating a 403 status code.

    This is confusing and makes things like the following overly complicated: Say, for example, you use a subdomain per user (a big reason to use wildcard DNS, apparently), and users have the ability to view each others profiles at username.example.com. This solution is confusing to the user and completely not what I want to do.

My ideal sollution will let the user know there is nothing to view at the url he entered. Preferably with a 404 and an error log entry for the address entered (not some other address).

Any help would be greatly appreciated!

share|improve this question
add comment

3 Answers

You can setup a default virtual host with the following according to the Apache docs:

RedirectMatch 404 /.*

http://httpd.apache.org/docs/2.2/mod/mod%5Falias.html#redirect

Let me know if it doesn't work.

share|improve this answer
add comment

This might help: I was trying to solve a wildcard subdomain problem I set myself earlier, and found some example code from webmasterworld (from the all-knowing jdMorgan) which seems to produce the behaviour you're after (404 on non-existent content):

#jdMorgan, again
#http://www.webmasterworld.com/apache/3906038.htm

# If the request is not for the the main domain or the www subdomain
rewriteCond %{HTTP_HOST} !^(www\.)?foo\.com
# get the requested subdomain name into variable %2
rewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.foo\.com
# and if not already rewritten to subdomain's subdirectory
rewriteCond $1 !^subdomains/
# internally rewrite to prefix "/subdomains/<subdomain-name>/" to the client-requested URL-path
rewriteRule ^(.*)$ /subdomains/%2/$1 [L]

If files exist in /subdomains/bar, and I navigate to http://bar.foo.com, the content displays. If I navigate to http://baz.foo.com, I get a 404 Not Found (and an additional informational 404, as I've not specified the ErrorDocument) - this also appears in the error_log.

Is this what you're after? Using this allows you to see the unavailable content in the error_log, or indeed create a 404 page solely for capturing the 404ed requests and logging them.

(Again, you have jdMorgan on WMW to thank for this, not me. That man really is a fount of knowledge!)

share|improve this answer
add comment

I recently had the same issue and could not find a soultion until now. The best option turns out to be using

Redirect 404 /

or (as suggested earlier)

RedirectMatch 404 /.*

directive in the default (first) vhost, which obviously must be unused. The vhost can potentially be a 'fake' one, so something like following at the beginning of your vhost config file:

<VirtualHost *:80>
ServerName default #fake name, unused domains default here
Redirect 404 /
</VirtualHost>

<VirtualHost *.80>
ServerName example.tld
........
</VirtualHost>

Smells like a hack though.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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