1

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>
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Quak
  • 11
  • 1

1 Answers1

0

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:

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]

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:

RewriteCond %{HTTP_HOST} ^(www\.)?dev.{site-1}\.com.?$ [NC] 
RewriteRule ^test/?$ https://www.dev.{site-1}.com/testredirect [R,L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84