Slim/Twig getBaseUrl() https issue

Hi guys,

Still a noob as it comes to PHP and learning a lot from all the messages.
I have some difficulties as it comes to some function (and completely understand them).

When I use the normal function (TwigExtension):

public function baseUrl()
        if (is_string($this->uri)) {
            return $this->uri;
        if (method_exists($this->uri, 'getBaseUrl')) {
            return $this->uri->getBaseUrl();

It returns

If I adjust the code to:

public function baseUrl()
        return '';

There is no problem. But in this case I have to adjust the file if I use it on localhost or production. So how could i make this more dynamic?

E.g. when https:// do not include the port 80 - that is the most important thing. And where do I need to put this piece of code.

Have a look at this thread and see if it pertains to your issue.

The Base URL can be relative (e.g. / or my-app/).

Make sure you have configured the Slim Twig Extension correctly.

$basePath = rtrim(str_ireplace('index.php', '', $container->get('request')->getUri()->getBasePath()), '/');
$twig->addExtension(new Slim\Views\TwigExtension($container->get('router'), $basePath));

Then add a name to your “root” route in routes.php:

$app->get('/', 'App\Controller\HomeController:indexAction')->setName('root');

Then try to add this code to your layout.twig file e.g.

        <base href="{{ path_for('root') }}"/>
1 Like

Hi Tim,

Saw the topic. Still have not found a good solution how to adjust the code.

If you are getting the port number 80, and HTTPS, then something is strange in your environment. What does $_SERVER look like? Are you intending to run under HTTPS? Are you behind any sort of reverse proxy or caching server?

Hi Tim,

I am not sure what is happening on the Server of the provider. I attend to run everything under a https (for a more secure) connection.

Here are the server details. Let me know if this is of any use?

Is can see that the [“HTTP_X_FORWARDED_PROTO”] is ‘https’ but the [“REQUEST_SCHEME”] is ‘http’ and the [“SERVER_PORT”] is ‘80’.
Don’t understand how I have to ‘read’ this combination of data and is this okay or wrong?

P.s. as Kalvn suggested in this ([SOLVED] Twig base_url() returns port 80 when used over HTTPS connection) topic it would not be handy to adjust code in the external libraries.
Although the issue is properly the combination of the server and the $request->getUri()->getBaseUrl(); function.

Is it possible to adjust this code for the (future) Slim libaries or perhaps some extra code to bypass this in the bootstrap\app.php instead? (don’t fully understand what is happening at the server side).

array(45) { 
["PATH"]=> string(28) "/usr/local/bin:/usr/bin:/bin" 
["HTTP_ACCEPT"]=> string(63) "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" ["HTTP_ACCEPT_ENCODING"]=> string(17) "gzip, deflate, br" 
["HTTP_ACCEPT_LANGUAGE"]=> string(23) "nl,en;q=0.7,en-US;q=0.3" 
["HTTP_CONNECTION"]=> string(10) "keep-alive" 
["CONTENT_LENGTH"]=> string(1) "0" 
["HTTP_COOKIE"]=> string(36) "" 
["HTTP_HOST"]=> string(19) "" 
["HTTP_USER_AGENT"]=> string(78) "" 
["HTTP_DNT"]=> string(1) "1" 
["HTTP_X_FORWARDED_PROTO"]=> string(5) "https" 
["HTTP_X_CLIENT_IP"]=> string(14) "" 
["HTTP_X_CLIENT_PORT"]=> string(5) "" 
["REDIRECT_UNIQUE_ID"]=> string(27) "" 
["REDIRECT_HTTPS"]=> string(2) "on" 
["REDIRECT_STATUS"]=> string(3) "200" 
["UNIQUE_ID"]=> string(27) "" 
["HTTPS"]=> string(2) "on" 
["SERVER_SIGNATURE"]=> string(0) "" 
["SERVER_SOFTWARE"]=> string(8) "Apache/2" 
["SERVER_NAME"]=> string(19) "" 
["SERVER_ADDR"]=> string(11) "" 
["SERVER_PORT"]=> string(2) "80" 
["REMOTE_ADDR"]=> string(14) "" 
["DOCUMENT_ROOT"]=> string(52) "" 
["REQUEST_SCHEME"]=> string(4) "http" 
["CONTEXT_PREFIX"]=> string(0) "" 
["CONTEXT_DOCUMENT_ROOT"]=> string(52) "" 
["SERVER_ADMIN"]=> string(25) "" 
["SCRIPT_FILENAME"]=> string(62) "" 
["REMOTE_PORT"]=> string(5) "" 
["REDIRECT_URL"]=> string(5) "/test" 
["SERVER_PROTOCOL"]=> string(8) "HTTP/1.1" 
["REQUEST_METHOD"]=> string(3) "GET" 
["QUERY_STRING"]=> string(0) "" 
["REQUEST_URI"]=> string(5) "/test" 

Slim looks to be doing exactly what it is supposed to do. The issue is your server configuration as you are running HTTPS under port 80, which is not standard. You will either need to change your server configuration to run under the standard HTTPS port (443), or perhaps set a Twig global variable at runtime with your desired base URL.

In instances such as yours, the most common reason is that your server environment is running behind some sort of caching server or CDN. So while the request is HTTPS, the front caching server is HTTPS, but Apache behind the scenes is running on port 80.

What I do in similar instances is described in this post.