1

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 :

app/design/frontend/vendor/theme/template/geturlhreflang.phtml:

<?php 
$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?

3

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

| improve this answer | |
  • 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 '17 at 6:07
0

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

app/design/frontend/mysite/mytheme/Magento_Theme/layout/default.xml

<body> 
    ...
    <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:

app/design/frontend/mysite/mytheme/Magento_Theme/templates/getlang.phtml

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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