How do I make Apache know which app directory to u

2020-07-27 16:41发布

问题:

I'm trying to create a site with CodeIgniter. I will have multiple domains served from this server, and what I am trying to do is to separate the HTTP requests for www.example1.com from the ones for www.example2.com and then redirect them to the their correct application folders.

Say this is my directory structure:

  • system
    • application
      • example1
      • example2

So this request

www.example1.com/gallery/

would then be redirected to exmaple1 folder.

Does anyone have any code example for doing this? Obviously you would need to use the ReWrite module...

I looked around the Apache documentation but I couldn't get anywhere. Let me know if you need more information on this.

回答1:

Using VirtualHost (and Alias, Rewrite) are the primary answers, but if you think you will be adding/removing other hosts using the same setup, then I would consider the mod_macro module. This is a third party module that will help you to simplify apache configuration and avoid copy/paste errors.

A simple example with your layout, define the following:

<Macro vh80 name>
 <VirtualHost *:80>
   DocumentRoot /system/application/$name
   ServerName www.$name.com
   CustomLog /system/application/$name/logs/access.log virtcommon
   ErrorLog /system/application/$name/logs/error.log
 </VirtualHost>
</Macro>

And then when enabling a site, use the following directive:

Use vh80 example1

Which will setup www.example1.com to use the configured directory as root as well as the configured logging directory.



回答2:

What you need is called VirtualHost to make www.example1.com and www.exaplme2.com point each to a different folder in your filesystem.

If you additionally wish to have a different path in the URI for serving the main content you have various alternatives:

  1. Physically create the folder and do no further changes

  2. Physically create a link (named gallery pointing to the main virtualhost root folder) to the folder and use the FollowSymLinks option for the VirtualHost

  3. Use an Alias directive in the VirtualHost

    Alias /gallery /
    
  4. Use mod_rewrite

    RewriteRule /gallery/(.*) /$1 [QSA]
    

The simplest (CodeIgniter permitting) would be the option 1 or 2.

Snippet from the VirtualHost documentation:

Running several name-based web sites on a single IP address.

Your server has a single IP address, and multiple aliases (CNAMES) 
point to this machine in DNS. You want to run a web server for 
www.example.com and www.example.org on this machine.

Note

Creating virtual host configurations on your Apache server does 
not magically cause DNS entries to be created for those host 
names. You must have the names in DNS, resolving to your IP 
address, or nobody else will be able to see your web site. 
You can put entries in your hosts file for local testing, 
but that will work only from the machine with those hosts 
entries.

Server configuration

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org

# Other directives here

</VirtualHost>

The asterisks match all addresses, so the main server serves no requests. 
Due to the fact that www.example.com is first in the configuration file, 
it has the highest priority and can be seen as the default or primary 
server. That means that if a request is received that does not match 
one of the specified ServerName directives, it will be served by this
first VirtualHost.


回答3:

This is really two questions:

  1. how to make www.example1.com and www.example2.com different? The answer to this is the VirtualHost directive.

  2. How to make /gallery point to example1? This is done with the Alias directive.