# Set site.url from env var (netlify deploy preview)

**URL:** <https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903>\
**Category:** Help\
**Created:** [February 9, 2020, 8:39pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903 "2020-02-09T20:39:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bolaurent](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bolaurent/32/2242_2.png) [@bolaurent](https://talk.jekyllrb.com/u/bolaurent)\
**Post date:** [February 9, 2020, 8:39pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/1 "2020-02-09T20:39:48Z")

</div>

I am new to jekyll, but I have built a site that is live, hosted on netlify.

> **[Sonoma County Matsuri Festival](https://sonomamatsuri.com)**
>
> »Sonoma County Matsuri Festival« is a nonprofit, volunteer organization whose mission is to promote Japanese Arts and Culture.

Now I would like to use netlify deploy previews so I can test some changes without modifying the main public site. Such deploy previews are published at an url which I cannot predict. The url is available in an environment variable.

I can’t figure out how to get this url into my build. It needs to be in a \_config.yml file, but I don’t know it in advance. I would like to override the site var in \_config.yml on the jekyll build command line, but that doesn’t seem to be possible.

Advice, please?

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:17pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/2 "2020-02-09T21:17:27Z")

</div>

how much do you need the actual site url for? I think I only use it for the canonical url in the metadata, every where else I just have my urls start with a / and the relative link.

Sorry, not an answer to your question, I know.

Looking at your site it looks like most of the urls are relative, so when the site is at [1234.mysite.com](http://1234.mysite.com) vs [https://mysite.com](https://mysite.com) what is actually not working?

---

<div class="post-metadata">

**Author:** ![bolaurent](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bolaurent/32/2242_2.png) [@bolaurent](https://talk.jekyllrb.com/u/bolaurent)\
**Post date:** [February 9, 2020, 9:19pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/3 "2020-02-09T21:19:20Z")

</div>

I used the [https://phlow.github.io/feeling-responsive/](https://phlow.github.io/feeling-responsive/) template. Without the actual url, lots of styling doesn’t work.

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:19pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/4 "2020-02-09T21:19:31Z")

</div>

in your head where you have:  
`<link rel="canonical" href="/about">`

This is where I think the actual site url is important - so I would add it there.

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:19pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/5 "2020-02-09T21:19:48Z")

</div>

what is a link to the preview site? I think it is something else.

---

<div class="post-metadata">

**Author:** ![bolaurent](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bolaurent/32/2242_2.png) [@bolaurent](https://talk.jekyllrb.com/u/bolaurent)\
**Post date:** [February 9, 2020, 9:20pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/6 "2020-02-09T21:20:52Z")

</div>

I think I have it figured out. This build command seems to have worked:

```auto
printf "site: %s" "$DEPLOY_URL" > _config_netlify.yml; jekyll build --config _config.yml,_config_netlify.yml

```

See my deploy preview at [https://5e40749f8f39b10008bd3868--angry-tereshkova-959566.netlify.com](https://5e40749f8f39b10008bd3868--angry-tereshkova-959566.netlify.com)

I have not yet verified that this produces a correct deployment of master branch to main url.

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:22pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/7 "2020-02-09T21:22:54Z")

</div>

ah - yeah that url I think would break the relative-ness of the urls. [not sure about that now]

it looks like you are able to have a different build command for the preview site? if so I think you are on the right track, writing the preview url to the dev config file.

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:26pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/8 "2020-02-09T21:26:29Z")

</div>

actually I still don’t see what good the site url is doing, none of the urls for css or js have that in them, they are all relative.

---

<div class="post-metadata">

**Author:** ![bolaurent](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bolaurent/32/2242_2.png) [@bolaurent](https://talk.jekyllrb.com/u/bolaurent)\
**Post date:** [February 9, 2020, 9:29pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/9 "2020-02-09T21:29:03Z")

</div>

just a moment; i will set the build command back to “jekyll build” and push another deploy preview.

---

<div class="post-metadata">

**Author:** ![bolaurent](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bolaurent/32/2242_2.png) [@bolaurent](https://talk.jekyllrb.com/u/bolaurent)\
**Post date:** [February 9, 2020, 9:36pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/10 "2020-02-09T21:36:16Z")

</div>

No, you are correct. Here is a new deploy preview made with just “jekyll build”. When I did this the other day, the styling of nav was all broken.

> **[Sonoma County Matsuri Festival](https://5e407a481afe500007e9da1e--angry-tereshkova-959566.netlify.com)**
>
> »Sonoma County Matsuri Festival« is a nonprofit, volunteer organization whose mission is to promote Japanese Arts and Culture.

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:38pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/11 "2020-02-09T21:38:26Z")

</div>

in your config files I think that if you DO NOT specify the url or baseurl it will probably work fine. If you do specify it the preview site may or may not break - I think it would be trying to pull the css from the prod site which may or may not work.

If you have any more trouble post a link to your repo if you can.

---

<div class="post-metadata">

**Author:** ![bolaurent](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/bolaurent/32/2242_2.png) [@bolaurent](https://talk.jekyllrb.com/u/bolaurent)\
**Post date:** [February 9, 2020, 9:39pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/12 "2020-02-09T21:39:42Z")

</div>

thanks for your help @rdyar.

I’m totally thrilled with how easy and powerful this technology is. I moved that site from a very creaky old (expensively hosted) wordpress site in two days.

---

<div class="post-metadata">

**Author:** ![rdyar](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/rdyar/32/710_2.png) [@rdyar](https://talk.jekyllrb.com/u/rdyar)\
**Post date:** [February 9, 2020, 9:40pm UTC](https://talk.jekyllrb.com/t/set-site-url-from-env-var-netlify-deploy-preview/3903/13 "2020-02-09T21:40:40Z")

</div>

SSG FTW!  
I used to use a CMS and static is so much easier to me.

Also, anyone reading this thread, not using the url or baseurl is not always good advice, probably best if you do use them unless there is a reason not to?
