How to affect kernel request hostname when bootstrapping from custom PHP script I'm bootstraping from a custom PHP script using:

$autoloader = require_once 'web/autoload.php';
$request = Request::create('/');
// $_SERVER['SERVER_NAME'] = 'whatever';
// $request-&gt;server-&gt;set('SERVER_NAME', 'whatever');
$this-&gt;kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$this-&gt;kernel-&gt;boot();
$this-&gt;kernel-&gt;prepareLegacyRequest($request);


When rendering a URL from that bootstrapped Drupal, via $item-&gt;link-&gt;getUrlObject()-&gt;toString(), it has a localhost hostname:

http://localhost/sites/default/files/styles/large/public/images/pages/1.JPG?itok=gSuAsMq-


I want to change that to whatever and I've tried what is commented out in the initial block which doesn't work. I've confirmed that the $request-&gt;server-&gt;set(..., ...); does set the value though.

Any idea on how to solve this?
You need to use the HTTP_HOST instead the SERVER_NAME, also you can pass it as $server param to the Request::create()

$autoloader = require_once 'web/autoload.php';
$request = Request::create('/', 'GET', [], [], [], ["HTTP_HOST" =&gt; 'whatever']);
$this-&gt;kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
$this-&gt;kernel-&gt;boot();
$this-&gt;kernel-&gt;prepareLegacyRequest($request);


Remember to add the hostname to $settings['trusted_host_patterns']