1

On my new Drupal 9 website, I customized the .htaccess file and the robots.txt file

I don't want its files to be overwritten when updating with Composer.

I added the lines below in my composer.json file but the .htassess file is overwritten every time. What's wrong with my code ? Thank you

"extra": { "drupal-scaffold": { "locations": { "web-root": "./" }, "file-mapping": { "[web-root]/.htaccess": false, "[web-root]/robots.txt": false } }, ... 
3
  • You may want to create a patch for your changes to the htaccess and robots.txt files and use composer to apply the patch rather than choosing to not update them. See How to patch your htaccess file correctly in composer.json Commented Jun 8, 2021 at 13:54
  • Same Issue. In my case: I have added few redirections on .htaccess in Drupal. when i run the composer install/update, the .htaccess is resets in Drupal 9. Commented Jan 27, 2022 at 13:03
  • 1
    This question is similar to: Composer keeps overwriting .htaccess (and other files) every time I do anything. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 16, 2024 at 14:14

1 Answer 1

3

Both .htaccess and robots.txt are a part of Drupal core, they are called "Scaffold Files". When you do composer update it will download the repository version of these files.

You have to re-add your custom changes every time you do composer update. Fortunately there is an automated way to do this:

Option 1. Append your changes:

 "name": "my/project", ... "extra": { "drupal-scaffold": { "file-mapping": { "[web-root]/robots.txt": { "append": "assets/my-robots-additions.txt", } } } } 

Option 2. Create patches:

 "name": "my/project", ... "scripts": { "post-drupal-scaffold-cmd": [ "cd docroot && patch -p1 <../patches/htaccess-ssl.patch" ] } 

Option 3. Exclude (not recommended):

The official example:

 "name": "my/project", ... "extra": { "drupal-scaffold": { "file-mapping": { "[web-root]/robots.txt": false } } } 

But this (option 3) approach is not recommended, because you might miss important core changes/security fixes.

The code, that you provided looks correct.

Make sure that you are using the https://github.com/drupal/core-composer-scaffold package and not the outdated https://github.com/drupal-composer/drupal-scaffold.

More details on Altering Scaffold Files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.