Drupal 8
For nodes you have to use hook_ENTITY_TYPE_view_alter
as that's the place where they got added to initially from NodeViewController::view()
.
And let me note that you are probably better off by simply redirecting all your incoming traffic to SSL by default: How to simply make the whole site HTTPS?
/**
* Implements hook_ENTITY_TYPE_view_alter().
*/
function MYMODULE_node_view_alter(array &$build, Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display) {
if (isset($build['#attached']['html_head_link'])) {
foreach ($build['#attached']['html_head_link'] as $key => $head) {
if ((isset($head[0]['rel']) ? $head[0]['rel'] : FALSE) == 'canonical') {
$url = \Drupal\Core\Url::fromRoute('<current>', [], ['absolute' => 'true'])
->toString();
$url = str_replace('https://', 'http://', $url);
$build['#attached']['html_head_link'][$key][0]['href'] = $url;
}
}
};
}
I just found out, that in the end we'll find all the head tags in hook_preprocess_html
in the $variables['page']['#attached']
array to be altered.
.htaccess
or Apache config. Problem solved.