How to Set Up Domain Access on Local using DDEV

Nitesh Sethia
Nitesh Sethia
blog image
DDEV
Domain Access

Recently, we have been working on a number of Drupal 9 website platforms leveraging Domain Access and have noticed some challenges across teams with getting these environment setups running smoothly on local. Today we are excited to share that knowledge and walk you through our guide to getting Drupal 9 Domain Access platforms up and running on local environments using DDEV!

Let's start by quickly answering the question "what is domain access?"

Domain Access is a contributed Drupal suite of modules that adds an additional routing layer within your Drupal application to logically segregate websites on your Drupal platform. In more non-technical speak - it allows you to setup and manage multiple website domains from a single Drupal login/interface. Core functionality of Domain Access allows you to separate access for content, users, taxonomy, menus, etc., by website domain.

There is a large contrib space around the Domain Access module, and there are many add-on modules in the space.

Why is Domain Access a challenge in local environments? 

Domain access can be a challenge on local environments because it extends the traditional Drupal site capabilities to support multiple domains. To get this set up and running on a local environment, you need to be able to configure multiple domains for the application to respond from in order to test the functionality you are building/working with.

Now for the fun part - let’s jump in!

Technology Stack

In order to setup Domain access on the Local machine, we are using the below technology stack:

Prerequisite

  • Local Drupal 9 website installed with DDEV.

Local Setup

  • Use composer to add the Domain Access module
  • Enable the Module on your local machine.
  • Once the module is enabled, we will have an option to add different sub domains.

Navigate to Configuration > Domains > Domain records.

Image of domain access in Drupal backend

  • Link - <site-link>/admin/config/domain
  • Click on Add Domain records button

image of adding a domain record in Drupal backend

  • Link - <site-link>/admin/config/domain/add
  • This will show up a form which will allow us to add a new sub-domain.

image of adding a domain record in Drupal backend

  • Fill in all the required configuration and Hostname will allow you to create new Sub Domains.
  • Save the changes and the new Sub Domain is ready to be used on your local machine.

Local Configuration

Once the Drupal instance is installed, update the additional_hostnames section in config.yaml. Location for config.yaml is drupal/.ddev/config.yaml. For more details click here.

name: drupal

additional_hostnames:
- "fr.drupal"
- "es.drupal"
- "it.drupal"

 

  • Restart the ddev instance using:
ddev restart

 

  • Configure cross-domain logins - Decide if you want users to stay logged in while moving across sites. If it is necessary, widen the scope of cookie_domain in services.yml from example.com to .example.com
parameters:
  session.storage.options:
    cookie_domain: '.example.com'
  • Configure domain-sensitive caching if necessary - Decide if you need content to not stay cached when moving across domains. If you need that, append url.site to required_cache_contexts.

That is it - pretty simple when you take a step back and look at it! We hope this walkthrough helps you in your journey to build it better using Domain Access. Was this helpful? We would love to know if you would like to see more of this type of content.

Drupal