Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using httpd-2.2.3-22 in RHEL 5.3.

How can I redirect to different document root in Linux based on the URL.


The document root for is /var/www/vhost/

If the URL is then it should point document root to /var/www/vhost/

If the URL is then it should point document root to /var/www/vhost/

Where web1 and web2 are the two different folders under the parent document root.

Please let me know how to configure apache in this situation ?

Warm Regards


share|improve this question

You need to configure virtual hosts for your server. There should be configuration files in /etc/httpd/conf on RHEL (maybe /etc/apache2 if you have a customized version). Open httpd.conf and edit the Virtual hosts part, near the bottom.

For instance, you could insert in the file (taken from the link below)

Listen 80

NameVirtualHost *:80

<VirtualHost *:80>
  DocumentRoot /var/www/vhost/

<VirtualHost *:80>
  DocumentRoot /var/www/vhost/

If Apache is listening on port 80 (http, default). Then you need to restart Apache ( /etc/init.d/httpd restart OR "apache2 restart" )

Please have a look at Apache Virtual hosts

Re: cannot add virtual hosts

If you cannot add virtual hosts, it is unlikely you can change further the apache and system configurations. The only solutions I see are

  • Use directly (web2)

  • If you do not mind using another port (ie not 80), you could install nginx which is pretty simple to configure, and that will accept a custom configuration file, and configure it to accept connections from port 5000 for instance, (eg ), provided that

    1. RHEL firewall accepts TCP connections from port 5000.
    2. if you are behind a router / firewall that port 5000 is accepted and routed to that web server
    3. that web1 / web2 are resolved via DNS, meaning the subdomains are declared and the IP is set to that web server (or router...)
  • hosts the document files at another web server, and update DNS for to have the web1/2 subdomains resolving to that new web server IP address

share|improve this answer
great answer. upboat. – troyengel Aug 21 '10 at 14:28

It sounds like you are looking for dynamic virtual hosts. There is a very good article in the Apache documentation on this:

Dynamically Configured Mass Virtual Hosting

There are multiple ways to do this, but the most flexible is probably mod_rewrite. Based on your example you could use the following virtual host configuration:

<VirtualHost *>
        ServerAlias *

        RewriteEngine on

        RewriteMap lowercase int:tolower

        # check the hostname is right so that the RewriteRule works
        RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9-]+\.testdomain\.com$

        # concatenate the virtual host name onto the start of the URI
        # the [C] means do the next rewrite on the result of this one
        RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]

        # now create the real file name
        RewriteRule ^([a-z0-9-]+)\.testdomain\.com/(.*) /var/www/vhost/$1/$2
share|improve this answer

Create two virtual hosts, each with their own DocumentRoot.

share|improve this answer
Hi, Thanks for the quick response. I do not have permission to create VirtualHost. There is only one VirtualHost created with server name "" which is pointing to "/var/www/vhost/" Is there are any other ways other than creating VirtualHost ? – Supratik Aug 21 '10 at 11:26

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.