7

After running composer update my Drupal installation gets updated, but it also changes the content of the .htaccess file each time.

How can I correctly patch it?

1
  • question was asked and answered by the same person at the same timestamp?
    – owenmck
    Commented Jul 21, 2023 at 13:21

2 Answers 2

7

Here is how I have successfully done it. This is for Drupal 8.9, but it should work for a typical composer setup.

Make sure you have patching enabled and ready to go, and know how to make a patch for drupal.

Short version:

Patch the core/assets/scaffold/files/htaccess file and scope the patch as 'drupal/core'.

Long Version:

There are probably variations in method, but this is the gist of it:

  1. Within the [web-root] folder create a temporary repo (if there is not already one there); e.g. cd html && git init
  2. Add the core file to the repo; e.g. git add core/assets/scaffold/files/htaccess
  3. Make your changes to core/assets/scaffold/files/htaccess and save it
  4. Save the patch; e.g. git diff core/assets/scaffold/files/htaccess > ../patches/htaccess.patch
  5. If you are not still using your repo, delete it; e.g. rm -Rf [web-root]/.git
  6. Move back to your composer directory
  7. Add the patch to the composer file "patches" section
"patches": {
  "drupal/core": {
      "Description of patch": "patches/htaccess.patch"
  }
}
  1. Run composer install

That should patch the core "htaccess" file, and then the normal scaffolding flow will copy that file over to your [web-root]/.htaccess file. Output should look similar to:

Gathering patches for root package.
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 1 install, 0 updates, 0 removals
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing drupal/core (8.9.13): Loading from cache
  - Applying patches for drupal/core
    patches/htaccess.patch (Description of patch)
...
Scaffolding files for drupal/core:
 - Copy [web-root]/.htaccess from assets/scaffold/files/htaccess
1
  • Thanks for this great explanation. On my website I use a different htaccess on the local environment than on my remote environment. Therefore I added the htaccess to my gitignore file. I need this patch to only work on my remote server. Is there a way to do this?
    – Joost
    Commented Apr 15, 2021 at 7:07
5

In the scripts section of your composer.json file, use the following:

"post-drupal-scaffold-cmd": [
  "patch -p0 < patches/htaccess.patch"
]

(this requires "drupal-composer/drupal-scaffold" in your require section)

3
  • Using drupal-composer/drupal-project I simply added one function to its ScriptHandler.php that always symlinks me my own .htaccess from the composer root dir as post-install-cmd and post-update-cmd into the web root dir.
    – leymannx
    Commented Oct 18, 2018 at 12:52
  • 5
    @leymannx The problem with replacing it completely is that you won't get any changes that are done in core, the patch will keep them unless they conflict
    – Berdir
    Commented Oct 18, 2018 at 14:57
  • 2
    And core .htacess changes are often (not always of course) security related, so it’s important to merge rather than overwrite changes
    – Clive
    Commented Oct 18, 2018 at 15:46

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.