我正在尝试使用 CodeIgniter 创建一个网站。我将从此服务器提供多个域,我想做的是将 www.example1.com 的 HTTP 请求与 www.example2.com 的 HTTP 请求分开,然后将它们重定向到正确的应用程序文件夹。

说这是我的目录结构:

  • 系统
    • 应用
      • 例子1
      • 例子2

所以这个请求

www.example1.com/gallery/

然后将被重定向到 exmaple1 文件夹。

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

我查看了 Apache 文档,但一无所获。如果您需要更多这方面的信息,请告诉我。

有帮助吗?

解决方案

使用 VirtualHost(和 Alias、Rewrite)是主要答案,但如果您认为您将使用相同的设置添加/删除其他主机,那么我会考虑 模组宏 模块。这是一个第三方模块,将帮助您简化 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 示例1

这将设置 www.example1.com 以使用配置的目录作为 root 以及配置的日志记录目录。

其他提示

你需要的东西叫做 虚拟主机 使 www.example1.com 和 www.exaplme2.com 分别指向文件系统中的不同文件夹。

如果您还希望在 URI 中使用不同的路径来提供主要内容,您有多种选择:

  1. 物理创建文件夹并且不做进一步的更改

  2. 以物理方式创建该文件夹的链接(名为 gallery 指向主虚拟主机根文件夹)并使用 关注符号链接 虚拟主机选项

  3. 使用 别名 VirtualHost 中的指令

    Alias /gallery /
    
  4. 使用mod_rewrite

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

最简单的(CodeIgniter 允许)是选项 1 或 2。

VirtualHost 文档的片段:

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.

这实际上是两个问题:

  1. 如何使 www.example1.com 和 www.example2.com 不同?这个问题的答案是 虚拟主机 指示。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top