apache - 使用 Apache 在另一个域上提供来自第一个域的子目录的内容
问题描述
我有一个网站,比如说domain1.example
,部署在带有 apache2 的 Amazon EC2 机器上。我想指出domain2.example
。
基本上是这样的:-> -> ->
(不改变浏览器地址栏中的 URL)domain1.example/path
domain2.example
domain1.example/path
domain2.example/one
domain1.example/path/one
domain2.example/two/page
domain1.example/path/two/page
我试过这些方法:
- 添加重写规则到
/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>
.htaccess
在项目文件夹中添加重写规则
RewriteCond %{HTTP_HOST} ^domain2\.example [NC]
RewriteRule ^(.*)$ https://domain1.example/path/$1 [P]
它们都不起作用。两个域都服务于根文件夹。难道我做错了什么?
解决方案
我将为具有不同文档根的两个域使用单独的虚拟主机来实现这一点。
<VirtualHost *:80>
ServerName "domain1.example"
DocumentRoot /var/www/html
...
</VirtualHost>
<VirtualHost *:80>
ServerName "domain2.example"
DocumentRoot /var/www/html/path
...
</VirtualHost>
虽然这将实现您的目标,但我不建议您设置这样的网站。
- 如果两个站点都有
.htaccess
文件,它们可能会相互冲突。在访问不同域上的内容时,您可能会看到不同的行为,因为这两种情况适用于哪些规则。 - 搜索引擎更喜欢仅在一个“规范”URL 上查找内容。当您在多个域上提供相同的内容时,它可能会混淆搜索引擎并损害您的 SEO。
推荐阅读
- svg - PWA InkCanvas 在 javascript 中将笔画保存为 svg
- botframework - QnAMaker 迁移后无法到达端点
- javascript - JScroll:在 ajax 更改选项卡和 URI 后更改 nextHref
- python - 熊猫:以一定的价值填补时间空白
- fonts - 如何在 VSCode 中使用自定义字体(是的,我已经看到了其他问题和用户设置,但它仍然不起作用)
- sql-server - 有没有办法在查询中结合 LIKE 和 BETWEEN 的功能?
- vba - VBA MS Word - 如果字符串包含大于 9999 的数字
- dictionary - Ansible 从 dict 获取值
- reactjs - TypeScript/React 在 currentvalue = 函数接口的情况下,扫描运算符如何工作?
- python - 使用 glob 通过 subprocess.run() 生成参数