Score:0

How can I add custom code to Drupals .htaccess and make sure it will stay there

jp flag

For one of my Drupal 9 website I need to add a custom code to the htaccess file to redirect the visitors from one subdomain to another for this I use the following code:

#Custom Rewrite Rule
RewriteBase /
RewriteCond %{HTTP_HOST} ^old-subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*) http://new-subdomain.mydomain.com/$1 [L,R]

I put this code directly under the RewriteEngine on on line 68.

But I know that updating my Drupal site might overwrite these lines of code. How do I make sure these changes to the .htaccess file are permanent?

unusedspoon avatar
aq flag
Might be worth explaining how you update your drupal code base. E.g. if you download and copy/paste manually or if you use composer? If it's composer you could look into creating a patch for .htacess which composer runs after an update
Score:2
id flag

Patching scaffolded files is documented precisely.


"name": "my/project",
  ...
  "scripts": {
    "post-drupal-scaffold-cmd": [
      "cd docroot && patch -p1 <../patches/htaccess-ssl.patch"
    ]
  }
id flag
If you need help creating a patch that is a different question which isn’t Drupal-specific.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.