如何让Apache知道要使用不同的领域,其应用程序目录?(How do I make Apache

2019-07-29 02:17发布

我试图创建一个笨网站。 我会从这个服务器服务的多个域,我试图做的是独立于药粥www.example2.com为www.example1.com的HTTP请求,然后将其重定向到其正确的应用程序文件夹。

之所以这样说,是我的目录结构:

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

因此,这要求

www.example1.com/gallery/

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

有没有人有这样做的任何代码的例子吗? 很明显,你需要使用重写模块...

我看着Apache文档周围,但我无法得到任何地方。 让我知道如果你需要更多这方面的信息。

Answer 1:

使用虚拟主机(和别名,重写)的主要答案,但如果你认为你会被添加/删除使用相同的设置其他主机,那么我会考虑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使用配置目录的根以及配置的日志目录。



Answer 2:

你需要的是所谓的虚拟主机 ,使www.example1.com和www.exaplme2.com点每到一个不同的文件夹中的文件系统。

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

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

  2. 物理上创建一个链接(称为画廊指向主要虚拟主机根文件夹)的文件夹,并使用了FollowSymLinks的虚拟主机选项

  3. 使用的别名在VirtualHost指令

     Alias /gallery / 
  4. 使用mod_rewrite

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

最简单的(笨允许的话)将是选项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.


Answer 3:

这实际上是两个问题:

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

  2. 如何使/库点EXAMPLE1? 这与Alias指令完成。



文章来源: How do I make Apache know which app directory to use for different domains?