2009-06-22 218 views
2

我正在尝试使用CodeIgniter创建一个站点。我将从该服务器提供多个域,我试图做的是将www.example1.com的HTTP请求与www.example2.com的HTTP请求分开,然后将它们重定向到正确的应用程序文件夹。如何让Apache知道哪个应用程序目录用于不同的域?

说这是我的目录结构:

  • 系统
    • 应用
      • 例1
      • 例题

所以这个要求

www.example1.com/gallery/

将被重定向到exmaple1文件夹。

有没有人有这样做的代码示例?显然你需要使用ReWrite模块...

我环顾了Apache的文档,但我无法获得任何地方。让我知道你是否需要更多的信息。

+0

@ lrussell810:请不要在编辑后追加“Thanks in Advance”。它被视为混乱http://meta.stackexchange.com/questions/97136/suggested-edits-add-thanks-in-advance – naveen 2012-01-01 16:15:19

回答

1

使用VirtualHost(和别名,重写)是主要答案,但如果您认为您将使用相同的设置添加/删除其他主机,那么我会考虑mod_macro模块。这是一个第三方模块,可以帮助您简化Apache配置并避免复制/粘贴错误。

一个简单的例子,你的布局,定义如下:

<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> 

然后启用站点时,请使用以下指令:

使用vh80 EXAMPLE1

哪将设置www.example1.com以使用配置的根目录以及配置的日志目录。

+0

哇。这很酷。我的另一个问题是Codeigniter的资产文件夹位于/ system/application/example1文件夹之外。它实际上与系统文件夹处于同一级别,它位于根目录。我仍在寻找一种方法来为来自每个域的img,js和css请求编写指令,并将它们重定向到assets/example1/js,assets/example1/css,assets/example1/img。 – picardo 2009-06-23 21:20:12

1

这实际上是两个问题:

  1. 如何使www.example1.com和www.example2.com不同?对此的回答是VirtualHost指令。

  2. 如何让/ gallery指向example1?这是通过Alias指令完成的。

2

你需要什么叫VirtualHost使www.example1.com和www.exaplme2.com点每到一个不同的文件夹中的文件系统。

如果你还希望有在URI不同的路径服务的主要内容,您有不同的选择:

  1. 物理上创建文件夹并做没有进一步的变化

  2. 物理创建链接(指向主虚拟主机根文件夹的名称库)到该文件夹​​并使用虚拟主机的FollowSymLinks选项

  3. 使用Alias指令在虚拟主机

    Alias /gallery/
    
  4. 使用mod_rewrite

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

最简单的(CodeIgniter的允许)会选择1或2

从虚拟主机文件摘录:

 
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. 
相关问题