My question is about using Magento2 HTML-head to generate hreflang links.

In Admin Content Configuration Store View HTML-Head Scripts & Styles I need the current URL for hreflang.

I have tried with following, but this does not work:

<link rel="alternate" hreflang="en" href="{{block class="Magento\Framework\View\Element\Template" template="Magento_Theme::geturlhreflang.phtml"}}" />

And in this file :


$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parsed_url = parse_url($actual_link);
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host   = isset($parsed_url['host']) ? $parsed_url['host'] : ''; 
$path   = isset($parsed_url['path']) ? $parsed_url['path'] : ''; 
echo "$scheme$host$path"; 

How can I get the current URL into the header?

2 Answers 2


Use Below code to get current URL

/** @var \Magento\Framework\UrlInterface $urlInterface */ $urlInterface = \Magento\Framework\App\ObjectManager::getInstance()->get('Magento\Framework\UrlInterface');

echo $urlInterface->getCurrentUrl();

i hope it will be help you

  • When I put your code in geturl.phtml and flush magento cache, I get the following result in the header: <link rel="alternate" hreflang="en" href="{{block class=" magento\framework\view\element\template"="" template="geturl.phtml" }}"> Is there something wrong with my {{ block class ="Magento\Framework\View\Element\Template" template="Magento_Theme::geturl.phtml"}}" directive in Magento Admin ?
    – rico
    Oct 31, 2017 at 6:07

I was able to solve the problem above, so I would like to share the answer.

Instead HTML-head section of Magento admin, I added into


    <block class="Magento\Framework\View\Element\Template" name="head.additional" as="head.additional" template="Magento_Theme::getlang.phtml"/>

And PHTML-File is in and looks:


<link rel="alternate" hreflang="en" href="https://en.mysite.com<?php echo parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);?>"/>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.