Setting Drupal 8 BASE_URL Solution

Abstract image

For security reasons the $base_url used in Drupal 7 has been removed in Drupal 8. $base_url was used to set the absolute URL for the installation which is then used to generate links. One such link is the one-time login link generated by the Drush command:

user:login.

Without $base_url the user:login command has no knowledge of the domain. A similar effect to setting the $base_url in D7 can be achieved in D8 by setting the Drush 'uri' option. This can be set in a number of places, drushrc.php or the drush.yml file (Drush 9 only). The reference link below is a great article on using drushrc.php. I will discuss using drush.yml.

Using drush.yml to set Drush Options 'uri'

The drush.yml file allows for valid statements for a Drush run time configuration. Drush started using yml files for configuration in version 9; earlier versions of Drush will never attempt to load a drush.yml file. It is also possible # to limit the version of Drush that will load a configuration file by placing the Drush major version number in the filename, e.g.

drush9.yml.

The reference link below points to example.drush.yml on Gitlab.

For the Drush user:login command to output a login link that includes the site URL we need to set the options 'uri' value in a drush.yml file. To test the value of Site URI run the command:

drush status

If Site URI has not been set the output will be:

Site URI : http://default

Running drush user:login will output something like:

http://default/user/reset/1/1561444r845/hp33d5678a...1L64zdmfnRcf8/login

Set up the drush.yml file

Create a drush.yml file and place it in one of the following directories:

  1. Drupal site folder (e.g. sites/{default|example.com}/drush.yml).
  2. Drupal /drush and sites/all/drush folders, or the /drush folder
    in the directory above the Drupal root.
  3. In any location, as specified by the --config (-c) option.
  4. User's .drush folder (i.e. ~/.drush/drush.yml).
  5. System wide configuration folder (e.g. /etc/drush/drush.yml 
    or C:\ProgramData\Drush\drush.yml).

Edit and include in drush.yml file the following:

#
# A Drush configuration file
#
# Docs at https://github.com/drush-ops/drush/blob/master/examples/example.drush.yml
#
# Edit or remove this file as needed.
# This section is for setting global options.
options:
  # Specify the base_url that should be used when generating links.
  # Not recommended if you have more than one Drupal site on your system.
  uri: 'https://yourdomain.com'

Clear the Drush cache:

drush cc drush

drush cr

Run drush status and the Site URI value should now be:

Site URI : https://yourdomain.com

Run drush user:login now and the output will include the installation URL:

https://yourdomain.com/user/reset/1/1561470845/hp...jLi2rH2m1L64zdmfnRcf8/login

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.