【问题标题】:Apache Virtual Host redirect to document root directoryApache 虚拟主机重定向到文档根目录
【发布时间】:2018-08-14 06:27:36
【问题描述】:

我已经成功地为给定的服务器名称设置了 DocumentRoot:

<VirtualHost *:80>
 DocumentRoot "/var/www/html/domain1"
 ServerName www.domain1.com
 ServerAlias *.domain1.com
#Other directives here
</VirtualHost>

我似乎不知道如何移动子文件夹,即“www.domain1.com/contact-us”,因此它显示位于“/var/www/html/domain1”中的 index.html。

我尝试在 VirtualHost 说明中添加 /contact-us 的别名:

<VirtualHost *:80>
     DocumentRoot "/var/www/html/domain1"
     ServerName www.domain1.com
     ServerAlias *.domain1.com
    #Other directives here
     Alias /contact-us /var/www/html/domain1
    </VirtualHost>

它没有按预期工作。

我正在考虑通过 domain1 的子文件夹中的 .htaccess 来执行此操作。如何将域名后的所有内容重定向到 index.html?

【问题讨论】:

  • 您能更准确地了解您的预期吗?你想要 domain.com/index.html 下的文件夹contact-us?
  • @DiogoJesus 当然。最终目标是将每个子目录重定向到通过虚拟主机(“/var/www/html/domain1”)设置的 DocumentRoot 中的主 index.html。所以 www.domain1.com/about-us 将指向“/var/www/html/domain1/index.html,以及其余 11 个子目录。

标签: html apache redirect virtualhost document-root


【解决方案1】:
RewriteEngine on
RewriteRule ^subdirectory/(.*)$ /$1 [R=301,NC,L]
RewriteRule ^about-us/(.*)$ /$1 [R=301,NC,L]
RewriteRule ^contact/(.*)$ /$1 [R=301,NC,L]
RewriteRule ^subfolder/(.*)$ /$1 [R=301,NC,L]

测试这段代码,确保更改文件夹名称。


编辑

这必须在您的 Web 目录根文件夹中的 .htaccess 中。

【讨论】:

  • 我在 /var/www/html/domain1 目录中创建了一个 .htaccess,但每次我去: www.domain1.com/about-us/ 它仍然显示索引.html 来自“默认”根目录,而不是 VH 中设置的 DocumentRoot。虚拟主机在访问 domain1.com 时工作正常并提供正确的内容 - 它显示来自“/var/www/html/domain1”的 index.html - 似乎 .htaccess 没有被 apache 读取?
  • 所以您没有从 /var/www/html/domain1/subdirectory 获取 index.hmtl 但从 /var/www/html 获取索引?我现在很困惑
猜你喜欢
  • 2015-09-05
  • 2011-12-15
  • 2018-08-17
  • 2014-02-17
  • 2021-02-08
  • 2016-02-02
  • 2016-07-16
  • 2018-07-08
  • 2012-01-27
相关资源
最近更新 更多