urls with http instead of https

Created on 9 January 2017, over 7 years ago
Updated 10 May 2024, about 2 months ago

Hi,

i'm running Drupal 8 as a cms next to a shop system.
drupal runs on a nginx behind a reverse proxy within a folder of a https-domain.

Shop-System: https://example.com/
Drupal 8: https://example.com/drupal/

Unfortunately, meta tags delivers http urls instead of the correct https version:

URL: https://example.com/drupal/

Wrong Meta-Tags:

<link rel="canonical" href="http://example.com/drupal/" />
<meta property="og:url" content="http://example.com/drupal/node" />
<link rel="shortlink" href="http://example.com/drupal/" />

Correct would be:

<link rel="canonical" href="https://example.com/drupal/" />
<meta property="og:url" content="https://example.com/drupal/" />
<link rel="shortlink" href="https://example.com/drupal/" />

URL: https://example.com/drupal/node/100000173

Wrong Meta-Tags:

<link rel="canonical" href="/drupal/node/100000173" />
<meta property="og:url" content="http://example.com/drupal/node/100000173" />
<link rel="shortlink" href="/drupal/node/100000173" />

Correct would be:

<link rel="canonical" href="/drupal/100000173" />
<meta property="og:url" content="https://example.com/drupal/node/100000173" />
<link rel="shortlink" href="/drupal/100000173" />

Within my settings.php, I added the following:

$_SERVER['HTTPS'] = 'on';
$conf['https'] = TRUE;

Maybe it's a bug or a wrong setting, can someone help to get the correct https-URLs?

Thanks!

๐Ÿ“Œ Task
Status

Needs review

Version

1.0

Component

Documentation

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia nitin.k INDIA

    Thanks #31

    In my case, we were using AWS Load Balancers.

    Regards..

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia dharmeshbarot Gujarat

    Hello,

    #34 works for drupal 9 also.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia n.a.t.j.b

    Hi, I wanted to add what worked for us on a fresh nginx, varnish and Drupal 10 install (10.2.3).

    Note: only core is installed so far, so no metatag or other modules causing confusion, but given this is the most in depth discussion around this topic, I'm posting here anyway in case it helps someone else save some time and frustration. The issue was apparent thanks to the rss feed link in the head tag on the "Welcome!" home page.

    i.e. <link rel="alternate" type="application/rss+xml" title="" href="http://example.com/rss.xml" />

    We set up the permanent (301) redirect in nginx from anything on port 80 to 443 so the page is served using https, but the absolute path for the rss link used http (all other paths are relative as expected).

    The "relevant" settings to fix the issue for our environment were:

    nginx

    server {
        listen 443 ssl http2;
        ...
        location / {
            ...
            proxy_set_header        X-Forwarded-Proto $scheme;
            ...
        }
    }

    Settings.php

    $settings['reverse_proxy'] = TRUE;
    $settings['reverse_proxy_addresses'] = array($_SERVER['REMOTE_ADDR']);
    $settings['reverse_proxy_trusted_headers'] = \Symfony\Component\HttpFoundation\Request::HEADER_X_FORWARDED_PROTO;

    Even though a few articles suggest it, the settings.php setting - $settings['reverse_proxy_proto_header'] = 'https'; - was stopping https from being used, even with the symfony setting enabled.

    Lastly, we didn't need to modify the Varnish config to assist with this (it's using one of the many Drupal config recipes) - I only mention it to help others avoid going down that rabbit hole as it was irrelevant to the solution in our case.

    Hope it helps, cheers.

  • ๐Ÿ‡ช๐Ÿ‡ธSpain pau.sanz

    Hi everyone!

    To alter the http protocol and instead set the https in the <link rel=โ€˜canonicalโ€™...> element. I have made use of the following hook, hook_page_attachments_alter. I'm using Drupal version 10.2.6 and PHP 8.1.27.

    function mytheme_page_attachments_alter(array &$attachments)
    {
        if (!empty($attachments['#attached']['html_head'])) {
            foreach ($attachments['#attached']['html_head'] as $key => $link) {
                if (isset($link[0]['#attributes']['rel']) && $link[0]['#attributes']['rel'] === 'canonical') {
                    $attachments['#attached']['html_head'][$key][0]['#attributes']['href'] = str_replace('http:', 'https:', $link[0]['#attributes']['href']);
                }
            }
        }
    }
    

    I hope I can be of help.

    Thank you,

Production build 0.69.0 2024