How can I have multiple apache sites under the same domain?

6,668

You have 2 solutions

1. Creating a Virtual Host for each site

In my opinion, I would recommend this solution if it's a possibility.

In this step, you would need need to create a site just as you did for your previous ones. But you will need to give them a domain like www.site3.temp even if it won't have a dns record. (notice the .tmp TLD)

Next, for each user that will open that site, they would need to edit their hosts file and add a line which points that temp url to your IP. Like so:

xx.xx.xx.xx      www.site3.tmp
  1. In Linux and Mac OS the hosts file is located at /etc/hosts
  2. In Windows it's located in <Windows Installation>/system32/drivers/etc/hosts
    By default, <Windows Installation> is in C:/Windows

2. Creating an Alias

If editing of the hosts file is not a solution, then this is you only other option

This would require you to edit your default virtual host file. It's the virtual host of www.mysite1.com, since it's the one that is opening when you go to http://xx.xx.xx.xx/mysite3.
This is the reason I don't like this solution since it makes stuff ugly and cluttered. And I only use it as a last resort.

In the Virtual Host configuration, add the following line (Changing the paths as required of course):

Alias /mysite3 /home/mysite3/
# Notice that there isn't a trailing slash
# So this won't work: Alias /mysite3/ /home/mysite3/

Of course, if you need to add some apache configuration to the directory, the also need to be done in the same host file.

<Directory /home/site3/>
    AllowOverride All # Just an example
</Directory>

3. (Bonus) Combining both solution

This is better then the second solution, but not as good as the first one

The default Virtual Host file is the first one loaded by apache. And by default, apache loads them alphabetically. So, what you can do, you can create a new virtual host file which would precede all your other virtual hosts files.
For example, 000-default (Which is now done in the default configuration of apache2.4).

Then create all your Aliases in that Host file, and the default URL of this virtual host would go to a 403 page. So in the end, you would get this:

www.mysite1.com   --> /home/mysite1
www.mysite2.com   --> /home/mysite2
xx.xx.xx.xx       --> 403 Error
xx.xx.xx.xx/site3 --> /home/site3
xx.xx.xx.xx/site4 --> /home/site4

This is preferred over the second solution, since that one might conflict with your other sites. For example, it might get messed up with your URL rewrites.

Share:
6,668

Related videos on Youtube

Joel Themmen
Author by

Joel Themmen

Updated on September 18, 2022

Comments

  • Joel Themmen
    Joel Themmen over 1 year

    I have a single IP that is external - say xx.xx.xx.xx. Currently, I have 2 urls that resolve to the IP address. I use virtual hosts within Apache to make sure that the right requests go to the site:

    www.mysite1.com --> /home/mysite1
    www.mysite2.com --> /home/mysite2
    

    Works great. However ...

    I want to add a 3rd (and then 4th and 5th, etc) site to this IP. The sites are temporary and thus won't get domain names. I was hoping to access them by using:

    http://xx.xx.xx.xx/mysite3 where mysite3 --> /home/mysite3
    

    but it seems that the virtual host from site1 catches these requests and routes the request to www.mysite1.com which struggles finding the files (mysite3) so I get the www.mysite1.com with 404 page (They are all Wordpress sites).

    I am sure that this can be done but I am just as sure I can't figure it out.

    Does anyone have an idea of how I need to configure "things"?

  • Joel Themmen
    Joel Themmen about 10 years
    Used solution #1. Worked perfectly and saved me hours. Much thanks ...
  • FireFaced
    FireFaced over 8 years
    Another way to go about this is to use the ~user feature of Apache. Create a folder called public_html in the /home/user folder, and add files to that.
  • Dan
    Dan over 8 years
    @FireFaced You are right! Though I don't recommend doing this on a live server, specially if you don't know what you are doing. You would need to enable the userdir module for apache; You should add that as answer by the way.