使用 .htaccess 将特定主机重定向到子目录而不影响不同的主机

Redirect specific host to subdirectory without affecting different hosts with .htaccess

是否可以将特定主机重定向到不同的子目录而不影响同一虚拟域中的其他主机? 例如:

www.example1.com -> www.example1.com/index.html
www.example2.com -> www.example2.com/test.html

两个域 运行 在同一个虚拟主机上,因此共享同一个 .htaccess 文件以及所有其他文件和目录。

提前致谢!

是的,使用 .htaccess 您可以使用 %{HTTP_HOST} 检查此人请求的 URL。例如:

# http://www.example1.com => http://www.example1.com/index.html
RewriteCond %{HTTP_HOST} ^www.example1.com [NC]
RewriteRule ^(.*)$ /index.html

# http://www.example2.com => http://www.example2.com/test.html
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ /test.html

您可以在操作中查看这些规则 here


编辑: 通过注释,为了防止循环,您可以确保我们没有请求 test.html

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example2.be [NC]
RewriteCond %{REQUEST_URI} !^/test.html
RewriteRule ^(.*)$ /test.html [R,L]