首页 > 解决方案 > 使用 Apache 在另一个域上提供来自第一个域的子目录的内容

问题描述

我有一个网站,比如说domain1.example,部署在带有 apache2 的 Amazon EC2 机器上。我想指出domain2.example。 基本上是这样的:-> -> -> (不改变浏览器地址栏中的 URL)domain1.example/path

domain2.exampledomain1.example/path
domain2.example/onedomain1.example/path/one
domain2.example/two/pagedomain1.example/path/two/page

我试过这些方法:

  1. 添加重写规则到/etc/apache2/sites-available/000-default.conf
...
<VirtualHost *:80>
        ServerName "domain2.example"
        ServerAdmin email@domain1.example
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        RewriteEngine On
        RewriteRule ^(.*)$ https://domain1.example/path/$1 [P]
        <Directory /var/www/html>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Require all granted
        </Directory>
</VirtualHost>
  1. .htaccess在项目文件夹中添加重写规则
RewriteCond %{HTTP_HOST} ^domain2\.example [NC]
RewriteRule ^(.*)$ https://domain1.example/path/$1 [P]

它们都不起作用。两个域都服务于根文件夹。难道我做错了什么?

标签: apache.htaccessamazon-ec2

解决方案


我将为具有不同文档根的两个域使用单独的虚拟主机来实现这一点。

<VirtualHost *:80>
    ServerName "domain1.example"
    DocumentRoot /var/www/html
    ...
</VirtualHost>
<VirtualHost *:80>
    ServerName "domain2.example"
    DocumentRoot /var/www/html/path
    ...
</VirtualHost>

虽然这将实现您的目标,但我不建议您设置这样的网站。

  • 如果两个站点都有.htaccess文件,它们可能会相互冲突。在访问不同域上的内容时,您可能会看到不同的行为,因为这两种情况适用于哪些规则。
  • 搜索引擎更喜欢仅在一个“规范”URL 上查找内容。当您在多个域上提供相同的内容时,它可能会混淆搜索引擎并损害您的 SEO。

推荐阅读