I'm trying to output the full asset URL. Using Craft 3.0.0-RC7.1

In .env:


In config/volumes.php:

'siteAssets' => [
    'path' => getenv('ASSETS_BASE_PATH') . '/assets/images',
    'url' => getenv('ASSETS_BASE_URL') . '/assets/images',

In my template:

<meta property="og:image" content="
{{ entry.socialSharingImage.one.getUrl('socialSharing') }}
" />


<meta property="og:image" content="

As you see, no site url at the start. Any ideas?

It seems the getUrl() method returns the path. Prepending it with {{ siteURL }} creates two /'s e.g. http://sitename.test//asseturl.jpg. Same results in dev and production.



You can slice the / from the end of {{ siteUrl }} and combine it with getUrl relative path:

<meta property="og:image" content="{{ siteUrl|slice(0,-1) }}{{ entry.fieldName.one().getUrl('kafel620') }}">

This will produce expected path:

<meta property="og:image" content="http://site.local/assets/images/ramPamPam/headers/_kafel620/tagiatelle.jpg">

Prepending it with {{ siteURL }} creates two /'s e.g. http://sitename.test//asseturl.jpg

It would do that if you've defined your site URL (either in Settings->Sites->Base URL or using the CRAFT_SITE PHP constant) with a trailing slash at the end of it.

i.e. http://sitename.test/

Changing it to http://sitename.test would work.

  • Thanks @Brad Bell - however using the @web alias forces a trailing slash upon saving the site settings page. Presumably I would have to set the URL explicitly to avoid this. May 10 '18 at 14:01
  • Also, explicitly setting the domain name in Site config also forces a trailing slash on save May 10 '18 at 14:01

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.