如何让 Apache 知道针对不同域使用哪个应用程序目录?

发布于 2024-07-24 22:54:40 字数 445 浏览 7 评论 0 原文

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

说这是我的目录结构:

  • system
    • 申请
      • 示例1
      • 示例2

因此,此请求

www.example1.com/gallery/

将被重定向到 exmaple1 文件夹。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡绿茶香 2024-07-31 22:54:40

您需要的是 VirtualHost 来创建 www.example1.com 和www.exaplme2.com 将每个都指向文件系统中的不同文件夹。

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

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

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

  3. 使用 虚拟主机中的别名指令

    别名 /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.

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.
同尘 2024-07-31 22:54:40

使用 VirtualHost(和 Alias、Rewrite)是主要答案,但如果您认为将使用相同的设置添加/删除其他主机,那么我会考虑 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 示例 1

这将设置 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:

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

ι不睡觉的鱼゛ 2024-07-31 22:54:40

这实际上是两个问题:

  1. 如何使 www.example1.com 和 www.example2.com 不同? 答案是 VirtualHost 指令。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文