Canonical url in laravel

不羁岁月 提交于 2020-03-05 06:05:41

问题


I'm using https://example.com version of url for my laravel built app site.

I want <link rel="canonical" hreflang="en-us" href="https://www.upscaleadventures.com/" /> as my canonical tag

{{url()->current()}} only gives the URL that I've setup as default. <link rel="canonical" hreflang="en-us" href="https://upscaleadventures.com/" /> version in my case.

How do I print https://www.upscaleadventures.com/ version of url in Laravel


回答1:


You may set APP_URL in your .env file to https://example.com, then access it via

env("APP_URL", "somedefaultvalue");

Edit

You may consider create a helper to convert your URL

function addWwwToUrl($url) {

   $bits = parse_url($url);

   $newHost = substr($bits["host"],0,4) !== "www." ? "www." . $bits["host"] : $bits["host"];

   $newUrl = $bits["scheme"]. "://" . $newHost . (isset($bits["port"]) ? ":" . $bits["port"] : "" ) . (isset($bits["path"]) ? ":" . $bits["path"] : "" ) . (!empty($bits["query"])? "?" . $bits["query"]: "");

   return $newUrl;
}

Reference

Usage

{{ addWwwToUrl(url()->current()) }}


来源:https://stackoverflow.com/questions/60023311/canonical-url-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!