Score:1

How do I avoid overwriting the .htaccess file when I use the Compose command?

mw flag

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
        }
    },
...
sonfd avatar
in flag
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](https://drupal.stackexchange.com/questions/271146/how-to-patch-your-htaccess-file-correctly-in-composer-json)
Thirsty Six avatar
gs flag
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.
Score:2
in flag

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
      }
    }
  }

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.

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

More details on Altering Scaffold Files.

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.