3

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?

2

2 Answers 2

1

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

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

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.