Pantheon Community

Ensure Remote Drush commands use the correct URI

This tip shows how to execute remote Drush commands with the correct HTTPS url provided by default for each environment leveraging drushrc.php.

Terminus is a very helpful tool that offers ways to execute site maintenance tasks on the command line, using Drush.

terminus remote:drush <site>.<env> -- <command> [options]

Or shorthand:

terminus drush <site>.<env> -- <command> [options]

Running remote tasks in Pantheon Multidev, Dev, Test and Live environments sometimes requires the correct domain name be provided to the Drush/Drupal PHP runtime. On Pantheon, the environment URI provided to the Drupal PHP runtime via Drush defaults to the Non-SSL domain: http://<env>-<site> But in the days of HTTPS Everywhere, this can have unintended consequences.

1. Manually provision drush with the --uri option

On can manually override the --uri option on the command line:

$ terminus drush <site>.<env> -- <command> --uri=https://<env>-<site>
$ terminus drush <site>.<env> -- <command> --uri=
$ terminus drush <site>.<env> -- <command> --uri=
$ terminus drush <site>.<env> -- <command> --uri=

However, using --uri for all remote drush commands makes documenting and/or running scripts in different environments cumbersome and error prone.

2. Automatically provision the uri option per environment

Drush has a way to store default options like the --uri option in a drushrc.php file, and if you commit the drushrc.php file in the correct location, then remote drush commands via terminus will take the file into account.

To ensure the URI is correct for all environments add the following snippet of code to the drushrc.php file:


 * Explicitly provision an SSL endpoint for Drush's --uri option in
 * Pantheon environments.
    case 'live':
      $uri = '';

    // Uncomment to use custom vanity domain on Test environment.
    # case 'test':
    #   $uri = '';
    #   break; 

     // Uncomment to use custom vanity domain on Dev environment.
    # case 'dev':
    #   $uri = '';
    #   break;

    // Lando environment
    case 'lando':
      $uri = '';  // @todo

    // Multidev environments
      $uri = "https://{$_ENV['PANTHEON_ENVIRONMENT']}-{$_ENV['PANTHEON_SITE_NAME']}";
  $options['uri'] = $uri;

Where to commit the drushrc.php file?

For Drupal 7 on Pantheon, the file can exist in any of the following locations:

/sites/all/drush/drushrc.php (recommended)

We typically avoid committing the /sites/default/drushrc.php location, so each developer can use this file for their own needs on their local environment.

For Drupal 8 projects on Pantheon, where your document root is typically not the repository root, the file can exist in any of the following locations:

/drush/drushrc.php  (recommended)

See also Pantheon documentation on Known Limitations with drush.

Feedback appreciated

So far, I’ve implemented this for a Drupal 7 project and as such, I haven’t had a chance to test this yet for Drupal 8/9 and so I would appreciate your feedback in that arena. Thanks!

A note about Lando:
People have told me that $_ENV['PANTHEON_ENVIRONMENT'] may be also set on local Lando environments, so I’ve added a line for this in the code snippet above. However, because I don’t use Lando, I’m not sure what values would be provisioned for the domain name there, or if Lando creates its own drushrc.php file in /sites/default/drushrc.php which would override any value set here.


This is so helpful! Thank you!

1 Like