如何让 Apache 知道针对不同域使用哪个应用程序目录?
我正在尝试使用 CodeIgniter 创建一个网站。 我将从该服务器提供多个域,我想做的是将 www.example1.com 的 HTTP 请求与 www.example2.com 的 HTTP 请求分开,然后将它们重定向到正确的应用程序文件夹。
说这是我的目录结构:
- system
- 申请
- 示例1
- 示例2
- 申请
因此,此请求
www.example1.com/gallery/
将被重定向到 exmaple1 文件夹。
有人有这样做的代码示例吗? 显然你需要使用 ReWrite 模块...
我查看了 Apache 文档,但一无所获。 如果您需要更多这方面的信息,请告诉我。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要的是 VirtualHost 来创建 www.example1.com 和www.exaplme2.com 将每个都指向文件系统中的不同文件夹。
如果您还希望在 URI 中使用不同的路径来提供主要内容,您有多种选择:
物理创建文件夹,不做进一步更改
物理创建该文件夹的链接(名为 gallery 指向主虚拟主机根文件夹)并使用 FollowSymLinks VirtualHost 选项
使用 虚拟主机中的别名指令
使用 mod_rewrite
最简单的(CodeIgniter 允许)是选项 1 或 2。VirtualHost
文档中的片段:
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:
Physically create the folder and do no further changes
Physically create a link (named gallery pointing to the main virtualhost root folder) to the folder and use the FollowSymLinks option for the VirtualHost
Use an Alias directive in the VirtualHost
Use mod_rewrite
The simplest (CodeIgniter permitting) would be the option 1 or 2.
Snippet from the VirtualHost documentation:
使用 VirtualHost(和 Alias、Rewrite)是主要答案,但如果您认为将使用相同的设置添加/删除其他主机,那么我会考虑 mod_macro 模块。 这是一个第三方模块,将帮助您简化 apache 配置并避免复制/粘贴错误。
一个简单的布局示例,定义以下内容:
然后在启用站点时,使用以下指令:
这将设置 www.example1.com 以使用配置的目录作为 root 以及配置的日志记录目录。
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:
And then when enabling a site, use the following directive:
Which will setup www.example1.com to use the configured directory as root as well as the configured logging directory.
这实际上是两个问题:
如何使 www.example1.com 和 www.example2.com 不同? 答案是 VirtualHost 指令。
如何让/gallery指向example1? 这是通过 Alias 指令完成的。
This is really two questions:
how to make www.example1.com and www.example2.com different? The answer to this is the VirtualHost directive.
How to make /gallery point to example1? This is done with the Alias directive.