How to get `drush` to use the correct URL?

Q: When I run drush status on production, it shows the hashed platformsh URL instead of my.domain.com

Site URI : https://master-7rqtwti-uenxsorgpc5rw.eu-2.platformsh.
site/

This means that links in emails generated by the system during cron, and other tasks like sitemap generation or logging are using the wrong URL.
How to tell the system to use the public URL?


A: This is a pretty common issue when running drush in any firewalled or routed-to environments. Drush running from the CLI doesn’t natively know what the appropriate hostname is.

We’ve supplied a small fix to support this for you with the utility script platformsh-templates/drupal8: drush/platformsh_generate_drush_yml.php. This fix pulls your routing information from the platfrom.sh environment, and makes it available to drush via a config file that drush uses.

If you’ve based your build on the Drupal 8 or 9 template, it should already be there. If you imported your project yourself, you probably need to check that this file is there, and that the line in the deploy hook that invokes it is also there.

hooks:
    deploy: |
        php ./drush/platformsh_generate_drush_yml.php

To verify, check your deploy log to confirm that it ran cleanly:

platform log deploy

...
Created Drush configuration file: /app/.drush/drush.yml

And check the file .drush/drush.yaml in the built environment to see that it worked.

platform ssh "cat /app/.drush/drush.yml"

# Drush configuration file.
# This was automatically generated by the script:
# /app/drush/platformsh_generate_drush_yml.php

options:
  # Set the default site URL.
  uri: "https://my.site.com/"

When it’s working, just a simple drush status should be enough to confirm that the URL is being correctly detected by the Drupal internals.

platform drush status

 Drupal version   : 9.0.2
 Site URI         : https://my.site.com/

In drush versions prior to (about?) 8.6, it used drushrc.php instead of drush.yml, and this helper script will not help if you’ve got drush older than that. Updating drush to something newer than 2017 should help.


Although it’s possible to force drush to use the correct URL by passing it the --uri= parameter every time, this is not a portable solution to hard-code into your .platformsh.app.yaml file, as it will now always be incorrect in your development branches. Don’t do that. Use the environment settings to supply your route URL instead.

Check **drush** uses the correct URL, you can follow the steps provided in the solution you mentioned

Check the **platformsh_generate_drush_yml.php** script: Ensure that the script **platformsh_generate_drush_yml.php** is present in your project. If you based your build on the Drupal 8 or 9 template, it should already be there. If you imported your project yourself, make sure the file is present in your project’s root directory

Check that the deploy hook in your .**platform.app.yaml** file invokes the **platformsh_generate_drush_yml.php** script. It should look like this

hooks:
  deploy: |
    php ./drush/platformsh_generate_drush_yml.php

Deploy your application with the changes to the **platformsh_generate_drush_yml.php** script and the deploy hook.

platform deploy

`
Once deployment is successful, SSH into your environment and check the content of the generated Drush configuration file .drush/drush.yml to make sure it includes the correct site URL.

platform ssh “cat /app/.drush/drush.yml”
``
Verify drush status…
Finally, run drush status to ensure that drush is using the correct URL
I hope the output should display the correct site URI.