Drupal 9 多站点拆分 .htaccess
我正在开发 Drupal 9 多站点,我想拆分 .htaccess
文件,每个站点一个文件来创建不同的重定向规则。
我知道可以在 drupal_root/.htaccess 上添加所有规则,但我更喜欢每个站点都有一个 .htaccess
。
结构为:
drupal_root/sites/site-1/
drupal_root/sites/site-2/
drupal_root/sites/site-3/
< code>drupal_root/sites/site-4/
并且我想添加 .htaccess
,例如在 drupal_root/sites/site-1/
和 drupal_root/sites/site-2/
中的另一个不同,
我尝试直接在 drupal_root/sites/site-1/
上添加 .htaccess
但它不起作用。
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.dev.{site-1}.com/test/?$ [OR]
RewriteCond %{HTTP_HOST} ^dev.{site-1}.com/test/?$ [NC]
RewriteRule ^(.*)$ https://www.dev.{site-1}.com/testredirect [R,L]
</IfModule>
I am working on Drupal 9 multisite and I want split the .htaccess
file, one per site to create different redirect rules.
I know that it's possible to add all rules on drupal_root/.htaccess but I prefer to have one .htaccess
per site.
The structure is:
drupal_root/sites/site-1/
drupal_root/sites/site-2/
drupal_root/sites/site-3/
drupal_root/sites/site-4/
and i want add .htaccess
, for example in drupal_root/sites/site-1/
and another different in drupal_root/sites/site-2/
I tried adding .htaccess
directly on drupal_root/sites/site-1/
but it doesn't work.
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.dev.{site-1}.com/test/?$ [OR]
RewriteCond %{HTTP_HOST} ^dev.{site-1}.com/test/?$ [NC]
RewriteRule ^(.*)$ https://www.dev.{site-1}.com/testredirect [R,L]
</IfModule>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据推测,用于访问“site-1”的 URL 实际上并不包含
/sites/site-1/
?在这种情况下,需要(由 Apache)内部将 URL 重写到此目录(或其子目录),以便(由 Apache)处理/sites/site-1/.htaccess
文件- 但我不相信这种情况会发生(因为一切都是由根目录中的核心 Drupal 安装处理的 - 进行“多站点”安装的目的)。所以,我认为你所建议的恐怕不太现实(无需更多工作)。
通常,您需要安装多个“单独的”Drupal 才能拥有单独的
.htaccess
文件。Drupal SE 站点上的以下问题似乎支持了这一点,该问题建议使用“重定向模块”来管理每个站点的重定向。
旁白:
无论如何,此规则不会执行任何操作,因为
HTTP_HOST
服务器变量仅包含请求中的主机名,而不是 URL 路径。但是,如果此.htaccess
文件仅在用户向“site-1”发出请求时进行处理,您是否需要检查主机名?例如,应该写成:
Presumably the URLs used to access "site-1" don't actually contain
/sites/site-1/
? In which case the URL would need to be internally rewritten (by Apache) to this directory (or a subdirectory of) for the/sites/site-1/.htaccess
file to be processed (by Apache) - but I don't believe that happens (since everything is handled by the core Drupal installation in the root - the purpose of having a "multisite" installation).So, I don't think what you are suggesting is realistically possible I'm afraid (without more work).
Ordinarily you would need to have multiple "separate" Drupal installations to be able to have separate
.htaccess
files.This would seem to be backed up by the following question on the Drupal SE site, which suggests to use the "Redirect module" to manage per site redirections.
Aside:
This rule wouldn't do anything anyway, since the
HTTP_HOST
server variable contains the hostname from the request only, not the URL-path. But would you need to check the hostname if this.htaccess
file is intended only to be processed when the user makes requests to "site-1"?For example, it should be written: