2

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.

Example:

The document root for testdomain.com is /var/www/vhost/testdomain.com/httpdocs

If the URL is http://web1.testdomain.com then it should point document root to /var/www/vhost/testdomain.com/httpdocs/web1.

If the URL is http://web2.testdomain.com then it should point document root to /var/www/vhost/testdomain.com/httpdocs/web2.

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

Supratik

3 Answers 3

5

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 *>
        ServerName testdomain.com
        ServerAlias *.testdomain.com

        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/testdomain.com/httpdocs/$1/$2
</VirtualHost>
3

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/testdomain.com/httpdocs/web1
  ServerName web1.testdomain.com
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot /var/www/vhost/testdomain.com/httpdocs/web2
  ServerName web2.testdomain.com
</VirtualHost>

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 http://testdomain.com/web1 (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 http://web1.testdomain.com:5000 ), 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 testdomain.com to have the web1/2 subdomains resolving to that new web server IP address

0
0

Create two virtual hosts, each with their own DocumentRoot.

1
  • Hi, Thanks for the quick response. I do not have permission to create VirtualHost. There is only one VirtualHost created with server name "testdomain.com" which is pointing to "/var/www/vhost/testdomain.com/httpdocs" Is there are any other ways other than creating VirtualHost ?
    – Supratik
    Aug 21, 2010 at 11:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .