问题
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