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

My httpd.conf looks like this:


DocumentRoot /var/www/html/site
ServerName www.example.com

 <IfModule mod_rewrite.c>
    RewriteEngine On

ServerName blog.example.com
DocumentRoot /var/www/html/blog

Listen *:2194

<VirtualHost *:2194>
   Redirect / http://mgr.example.com/

I want to be able to enable 'categories' as subdomains, ie if a user accesses:


It should show him that URL itself, but show him the contents of www.example.com/science.php

It does not have to be a generic / wildcard based approach - these categories will be a small number that I can specify individually. I just dont want whatever I add to interfere with the existing functioning of the site.

How can I do this?

share|improve this question

1 Answer 1

up vote 2 down vote accepted


ServerAlias *.example.com

to the vhost and employ these rewrite rules:

Options +FollowSymLinks 
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule (.*) http://www.example.com/%1 [R=301,L]

If you already have a subdomain, eg. subsub.example.com, explicitly set as ServerName/ServerAlias for another vhost, it will take precedence over the wildxard vhosts

share|improve this answer
Hi. If I want specifically for it to react on science.example.com - how do I do that? –  siliconpi Dec 28 '11 at 16:11
Add ServerAlias science.example.com instead, and change the first Rewrite Condition to: RewriteCond %{HTTP_HOST} ^(science)\. [NC]. This way you're saying "If the hostname starts with science then:" instead of the first example where the condition is true everytime the hostname "does NOT start with www " –  Mathias R. Jessen Dec 28 '11 at 16:46
If you don't want anyone to access your site (and get redirected), from other hostnames than the www and the few subdomains you specify, I would probably go with the "wildcard" rewrite rules, where you rewrite all requests that does NOT start with "www", and then ONLY add the subdomains needed as ServerAlias's –  Mathias R. Jessen Dec 28 '11 at 16:49
Hmm - thanks for responding... I'm trying the original and OpenDNS is redirecting me to its own page. The domain is registered at one place and the hosting is at the other. Do I need to add any DNS entries? –  siliconpi Dec 28 '11 at 16:52
Yeah, create a CNAME record for each subdomain and point them to the www. subdomain –  Mathias R. Jessen Dec 28 '11 at 17:52

Your Answer


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.