Ad

Canonical Url In Laravel

- 1 answer

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

I want <link rel="canonical" hreflang="en-us" target="_blank" rel="nofollow noreferrer" 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" target="_blank" rel="nofollow noreferrer" href="https://upscaleadventures.com/" /> version in my case.

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

Ad

Answer

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()) }}
Ad
source: stackoverflow.com
Ad