1

How to configure Trusted_Host_Settings for subdomains?

The documentation does not give an example of a subdomain.

For example, I have an Add-On domain that points to website.info, but the subdomain is mainsite.com/website.info

1
  • Did not work. Invalid hostname for this computer. Perhaps using the term subdomain caused confusion. The pattern I have to match is: domain.com/subdomain.info Sites in folders under the main domain.
    – David
    Apr 28, 2018 at 17:58

1 Answer 1

6

It's actually documented in settings.php file as in examples:

Drupal 8.5.x
https://cgit.drupalcode.org/drupal/tree/sites/default/default.settings.php?h=8.5.x#n712

<?php
$settings['trusted_host_patterns'] = [
  '^example\.com$',           // Matches naked domain only.
  '^.+\.example\.com$',       // Matches any subdomains.
  '^subdomain\.example\.com', // Matches a specific subdomain.
];

You can easily define any naked or sub-domain, as long as defined regex pattern matches them correctly.

4
  • Good catch The other things I have seen did not provide a pattern like this. Thanks.
    – David
    Apr 27, 2018 at 21:43
  • Did not work. domain.com/subdomain.info Matching the folder structure of sites under the main site.
    – David
    Apr 28, 2018 at 18:01
  • You really should read the documentation for the trusted_host_patterns setting. As the name suggests, it's for setting up trusted hostnames. If you don't know the difference between host and path, I also recommend reading up What is a URL? at MDN.
    – osman
    Apr 28, 2018 at 18:38
  • I found when using trusted_host_patterns in a development area with a deeper structure, I needed to add multiple lines like so: $settings['trusted_host_patterns'] = array( '^example\.com$', '^work\.example\.com$', '^dev\.work\.example\.com$', ); Sep 22, 2021 at 15:10

Your Answer

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

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