Score:2

What is the proper process to customize htaccess files?

cn flag

We have custom redirect links that use the RedirectMatch rules that redirect based on a root path. We'd rather handle these at the web server level rather than using the Redirect module (if it can even do match based rules), so we put these rules in the .htaccess file, e.g.:

RedirectMatch 301 ^/computing/hpc[/]?(.*) https://hpc.our.domain/$1

Since the .htaccess file is updated with a composer install because of the scaffolding, what is the best process for adding our rules to the file? I’m not looking to prevent the scaffolding from updating the .htaccess file, but I may need to modify our patch from time to time so that it is applied to the newest version.

How do you go about editing and updating the patch when something needs to get added or removed?

leymannx avatar
ne flag
Does this answer your question? [Composer keeps overwriting .htaccess (and other files) every time I do anything](https://drupal.stackexchange.com/questions/290989/composer-keeps-overwriting-htaccess-and-other-files-every-time-i-do-anything)
shelane avatar
cn flag
Not really. I have edited my question so that it hopefully more clear.
Score:1
cn flag

Depending on level of customization, there are two strategies you could employ.

  • Patch drupal/core with a modified htaccess file including your customization.
  • Override the htaccess file via scaffolding.

If you choose the first option here is a really easy way to do it.

  1. Checkout the version of Drupal you are using to a folder.
  2. Adjust the htaccess file in assets/scaffold.
  3. Run git diff > htaccess.patch.
  4. Go back to your project and create a patches folder, place the new file inside.
  5. Add the following to composer.json extra section (make sure you have installed cweagans/composer-patches):
"patches": {
  "drupal/core": {
    "Add redirects": "patches/htaccess.patch"
  }
}

If you choose option two:

  1. Create an assets/scaffold folder(s) in your project.
  2. Copy the htaccess file inside of it.
  3. Make your adjustments to the file.
  4. Add the following to composer.json extra section:
"drupal-scaffold": {
  "locations": {
    "web-root": "web/"
  },
  "file-mapping": {
    "[web-root]/.htaccess": "assets/scaffold/.htaccess"
  }
}
Score:0
cn flag

Ryan Hartman's answer was great. We ended up taking a slightly different route. As with Drupal, there are often many ways of doing things.

We added this section to our composer.json file:

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

Our .htaccess file is still part of our git repo. Each time we need to edit the rules, we make all the necessary changes to the .htaccess file and then we run a bash script (actually a Docksal custom fin command) that generates the patch file. The script looks like this:

mkdir temphtaccess
cd temphtaccess
git init
cp "${DOCROOT_PATH}/core/assets/scaffold/files/htaccess" "${PROJECT_ROOT}/temphtaccess/.htaccess"
git add .
git commit -m 'Scaffold htaccess commit'
cp "${DOCROOT_PATH}/.htaccess" "${PROJECT_ROOT}/temphtaccess/.htaccess"
git diff > ../patches/htaccess.patch
cd ${PROJECT_ROOT} && rm -rf temphtaccess
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.