我如何在 Apache httpd.conf 中配置以伪装同一文件夹中的不同主页文件,就好像它们位于不同文件夹中一样?

How can I config in Apache httpd.conf to disguise different homepage files in the same folder as if they were in different folders?

我有一个文件夹结构如下的项目:

/www
    /img
    /css
    /js
    /templates
    index1.html
    index2.html
    index3.html

如上,我有三个主页文件。因为它们有很多共同的部分,所以它们在一个项目文件夹中。所以实际上,它们属于一个项目。但是我希望人们使用这样的 url 访问这个网站:

http://www.servername.com/project1/
http://www.servername.com/project2/
http://www.servername.com/project3/

好像它们是三个不同的项目。因为我不想向人们展示这只是一个项目。我希望它们在三个不同的项目中看起来像,并且三个主页文件具有相同的名称 (index.html)。

所以我必须在 httpd.conf 文件中配置虚拟目录。我在 httpd.conf 文件中放置了三个 <IfModule dir_module> 节点,但它似乎不起作用:

<IfModule dir_module>
    DirectoryIndex index1.html

    Alias /project1/ "E:/www/"
    <Directory "E:/www/">
        ......
    </Directory>
</IfModule>

<IfModule dir_module>
    DirectoryIndex index2.html

    Alias /project2/ "E:/www/"
    <Directory "E:/www/">
        ......
    </Directory>
</IfModule>

<IfModule dir_module>
    DirectoryIndex index3.html

    Alias /project3/ "E:/www/"
    <Directory "E:/www/">
        ......
    </Directory>
</IfModule>

那我该怎么做才能达到这个目的呢?

mod_alias

https://httpd.apache.org/docs/2.4/mod/mod_alias.html

您可以将它与重写和 LocationMatch 指令结合使用,以实现所有子项。 但是,您使用的 'disguise' 这个词让我觉得您真的只需要阅读 .htaccess 手册:

https://httpd.apache.org/docs/current/howto/htaccess.html

实际上最好配置单独的主机名,因此 project1.example.comproject2.example.com 等等。您可以在您的 http 服务器配置中将它们配置为虚拟主机。除此之外,您唯一需要的是 A 记录的 "wildcard DNS resolution"。

但是如果你真的想使用路径来分隔,那么最简单的方法是在文件系统中使用符号 link,这将允许对这些路径进行单独配置。因为看起来你在 MS-Windows 上,你没有那个选项,那个系统不提供这样的功能。所以你必须尝试解决...

由于每个文件夹只能有一个 DirectoryIndex 指令,因此您的方法行不通。但是由于您的 "projects" 显然只包含一个静态 html 文件,为什么不相应地更改 Alias 指令呢?

Alias /project1 "E:/www/index1.html"
Alias /project2 "E:/www/index2.html"
Alias /project3 "E:/www/index3.html"
DocumentRoot "E:/www"
<Directory "E:/www/">
    # Whatever options you need for the common folder
</Directory>

另一种方法是使用 "internal rewriting"。显然,您需要加载并激活重写模块 (mod_rewrite)。然后你可以这样做:

RewriteEngine on
RewriteRule ^/?project1 /index1.html [END]
RewriteRule ^/?project2 /index2.html [END]
RewriteRule ^/?project3 /index3.html [END]
DocumentRoot "E:/www"
<Directory "E:/www/">
    # Whatever options you need for the common folder
</Directory>

可能 导致加载 css 文件等资产时出现问题。这取决于您网站内部参考的结构。

注意:如果您遇到 http 状态 500 ("internal server error"),请查阅您的 http 服务器错误日志文件以查找具体问题。很可能您操作的是非常旧版本的 apache http 服务器,并且 [END] 标志不可用。在这种情况下,请尝试使用 [L] 标志。

重写模块比别名模块提供了更多的灵活性,但也增加了复杂性。