Apache2 – Set Up Apache2 to Serve Multiple Directories on the Same Domain Based on URL Path

apache-2.2linuxvirtualhost

I want to host two different sites on the same IP, same server, and same domain using Apache2 on Ubuntu Linux. Let's say I have the following directories which represent sites that will serve content:

/srv/www/blog/          #for the blog
/srv/www/mainsite/      #for the main site

I want to set up Apache2 so that the following applies:

  1. When a user visits http://mysite.com/ they are served content from /srv/www/mainsite/.
  2. When a user visits http://mysite.com/blog, they are served content from /srv/www/blog/.

I had thought I was going to be setting up multiple virtual hosts, but only seems to apply for distinct domains and/or IPs. What do I need to do in my apache2 configuration to achieve the above functionality?

Best Answer

Inside your <VirtualHost>:

DocumentRoot /srv/www/mainsite
<Directory /srv/www/mainsite>
    Order Allow,Deny
    Allow from all
</Directory>

Alias /blog /srv/www/blog
<Directory /srv/www/blog>
    Order Allow,Deny
    Allow from all
</Directory>

Ignore the directory blocks if you already have one that covers /srv/www - I just put them in to make it clear that you'll need Apache's access controls allowing access to both.

Related Topic