Drupal 8: trusted_host_patterns error in settings.php

In my Drupal 8 install I was getting an error about not setting the trusted_host_patterns variable not being enabled, in settings.php. This setting is not under D8 admin control. You need to directly edit the settings.php file. Ok, not difficult.

Unfortunately I was getting a permissions error every time i tried to edit the file. I tried changing the permissions but to no avail. It seems like permissions for the folder above was set too low, so I had to change that first, before I was able to save my changes to settings.php. So I then log in to D8 to see if the change cleared the error. At this point it looks like D8, or my host, reset the folder and settings.php permissions to default. For me, permissions need to be reset every time I need to do a change.

As well, it seems like the required changes need to be placed after a */ and before a /*. I noticed in my editor that the code did not change to blue, like the rest of the code, but thought nothing of it, until I read ,a href=”https://www.drupal.org/node/2758845″>this thread and post by stuhannaford:

  1. File permissions – Make sure the settings.php file is set to 644 whilst editing, then revert back to 444 once finished. I had an issue with permissions in cPanel and eventually went via FTP which seemed to get around it. Really odd one this as cPanel kept reverting my file permissions to 444 from 644 and thus the changes weren’t actually being saved properly.
  2. DON’T just uncomment the lines in the settings example, you MUST copy it out and then place under the line */ and above the line /** on roughly lines 726 and 728

All this for a tiny piece of code, but it did clear my error:

$settings['trusted_host_patterns'] = array(
'^www\.example\.com$',
'^example\.com$',
);

Leave a Reply

Your email address will not be published. Required fields are marked *