Overwriting base_href in SilverStripe 4

There are use-cases which require to overwrite the URL the SilverStripe base-tag <% base_href %> defined in the theme. Usually this is the case if you are using a hosting environment which doesn't allow you to control the header parameters passed through by your web-server to the PHP run-time environment. For example this could be required if you are keen to enforce a SSL-only hosting setup behind a firewall or CDN.

There is a simple way to overwrite the base-href URL. The following configuration lives within a newly created file called "app/_config/baseurl.yml":

---
Name: baseurl
Only:
  environment: 'live'
---
SilverStripe\Control\Director:
  alternate_base_url: "https://yournewurl.xyz" 

This will ensure your base href URL is set to the defined URL, here "https://yournewurl.xyz".

Keep in mind, you will need to set the SS_ENVIRONMENT_TYPE in your local .env-file to dev to ensure this doesn't apply on your local development environment:

SS_ENVIRONMENT_TYPE="dev"