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
        }
    },
...
2
  • 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
    – sonfd
    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 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.