How to set up a Wildcard (catch-all) Virtual Host in Apache

Suppose that you need to set up a virtual host in Apache that catches all un-matched traffic coming in to your server. There are a number of uses for this, but the main idea is that you have a number of other virtual hosts set up, but you want anything that doesn’t match those to forward to a specific virtual host.

Here is an excerpt from an example httpd.conf file:

  1.  
  2. <VirtualHost *:80>
  3.  
  4.     ServerName default
  5.     ServerAlias *
  6.     ServerAlias somepreviewdomain.com
  7.  
  8.     DocumentRoot /home/me/my_app/html
  9.  
  10.     <Directory "/home/me/my_app/html">
  11.         Options Indexes FollowSymLinks
  12.         AllowOverride All
  13.         Allow from All
  14.     </Directory>
  15.  
  16. </VirtualHost>

Just be sure to make this the last virtual host entry in your httpd.conf file, so that any other vhosts before that work as they should.

In this example I also included an alias for an actual domain so that the vitualhost can still be accessed by an actual domain as well.

Tags:

About marc

a Senior Software Engineer in Los Angeles, CA. He likes to receive comments :)