Quick Virtual Hosting

| | Comments (0) | TrackBacks (0)

Here's a quick docblog of httpd.conf settings for configuring Apache to virtual host an unlimited number of domain names.

Incoming domain names are translated to /{first letter of}/{domain name}/. This structure works great when hosting thousands of sites.

For example, a reqeust to either www.example.com or example.com will be retrieved out of /var/virtual-hosts/sites/e/example.com/.

  Listen 8000
  UseCanonicalName Off
  ...

<VirtualHost *:8000>

    DocumentRoot /var/virtual-hosts

    RewriteEngine On
    RewriteMap lowercase int:tolower

    ## prepend and flatten incoming server name
    ## save original path for trailingn slash redirect
    RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [E=ORIG_PATH:$1]
    
    ## strip the www from the domain name
    RewriteRule ^www\.(.+)$  $1

    ## converts xxxx.tld and www.xxxx.tld to local dir structure
    RewriteRule (([a-z0-9])[-.a-z0-9]*)/(.*)$  /sites/$2/$1/$3

    ## forces a redirect if there is no trailing slash
    ## this is an artifact of serving from vhosts switched on ports instead
    ## of ip's
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d
    RewriteRule ^(.+[^/])$  http://%{SERVER_NAME}%{ENV:ORIG_PATH}/  [R]

</VirtualHost>

Note that I use ip to port mapping via big-ip. This way I can have one configuration shared across many Apache instances, allowing them to serve content from different content stores.

Here is a more thorough treatment on virtual hosting (1.3) via the Apache site.

0 TrackBacks

Listed below are links to blogs that reference this entry: Quick Virtual Hosting.

TrackBack URL for this entry: http://www.manamplified.org/cgi-bin/mt-tb.cgi/287

Leave a comment