# Relative URL and BaseURL

**URL:** <https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051>\
**Category:** Help\
**Created:** [August 13, 2018, 9:09am UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051 "2018-08-13T09:09:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yashumittal](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/yashumittal/32/1260_2.png) [@yashumittal](https://talk.jekyllrb.com/u/yashumittal)\
**Post date:** [August 13, 2018, 9:09am UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/1 "2018-08-13T09:09:22Z")

</div>

I facing difficult understanding `relative_url` and `baseurl`.

I have deployed a site on gitlab pages and in I have used `relative_url` in all the links but I didn’t use `baseurl` in the `_config.yml` file and it didn’t work.

But when I deployed the same on netlify it works.

* * *

If I use baseurl, for example: `baseurl: /blog/`, and along with that if I use `relative_url` in the links then it append the baseurl to the links.

Here’s how it looks:

URL: [https://example.com/blog](https://example.com/blog)

then the link will be: /blog/assets/css/style.css

**NOTE:** I have already read the docs

---

<div class="post-metadata">

**Author:** ![mmistakes](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/mmistakes/32/2169_2.png) [@mmistakes](https://talk.jekyllrb.com/u/mmistakes)\
**Post date:** [August 13, 2018, 1:21pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/2 "2018-08-13T13:21:39Z")

</div>

The `relative_url` filter adds in whatever you’re using as `baseurl` in your `_config.yml`. There’s no point to us it if you’re already hardcoding that value into your links.

`absolute_url` filter works similar but takes `site.url` + `site.baseurl` and prepends URLs with them.

---

<div class="post-metadata">

**Author:** ![yashumittal](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/yashumittal/32/1260_2.png) [@yashumittal](https://talk.jekyllrb.com/u/yashumittal)\
**Post date:** [August 13, 2018, 1:29pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/3 "2018-08-13T13:29:47Z")

</div>

> [@mmistakes](#):
>
> There’s no point to us it if you’re already hardcoding that value into your links.

What this means? Can you explain it?

---

<div class="post-metadata">

**Author:** ![ginfuru](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/ginfuru/32/773_2.png) [@ginfuru](https://talk.jekyllrb.com/u/ginfuru)\
**Post date:** [August 13, 2018, 4:07pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/4 "2018-08-13T16:07:54Z")

</div>

for example – you have an image and you’re adding the full path to the `src`

**Input**

```auto
<!-- baseurl is set to "/this_is_my_site" in _config.yml -->
{{ '/images/dog.jpeg' | relative_url }}

```

**Output**

```auto
/this_is_my_site/images/dog.jpeg

```

---

<div class="post-metadata">

**Author:** ![yashumittal](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/yashumittal/32/1260_2.png) [@yashumittal](https://talk.jekyllrb.com/u/yashumittal)\
**Post date:** [August 14, 2018, 2:24pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/5 "2018-08-14T14:24:49Z")

</div>

What if I don’t use `baseurl` and `relative_url`?

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [August 14, 2018, 2:32pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/6 "2018-08-14T14:32:04Z")

</div>

@yashumittal `baseurl`, `relative_url` are two different entities.

When you set `baseurl: "/blog"`, Jekyll considers `/blog/` as the root of your website. i.e. Instead of having your site served at `http://localhost:4000/`, you’ll have it served at `http://localhost:4000/blog/` instead.

Jekyll expects you to code links in your templates in the same fashion. This is why you would see some templates containing variations of the following:

```auto
<link rel="stylesheet" href="{{ 'css/style.css' | prepend: site.baseurl }}" />

```

```auto
<link rel="stylesheet" href="{{ site.baseurl }}/css/style.css" />

```

There is a problem with the above usage though. Users can configure _`baseurl:`_ to be a string with containing variable numbers of slashes (e.g., `'/'` or `'/blog/'` or `'blog/'` or `'/blog//`'). Jekyll wouldn’t care about the values set and would simply result in improper links in their HTML.

That’s when Jekyll introduced **Liquid Filters: `relative_url` and `absolute_url`**

`relative_url` ensures proper links by:

- ensuring the url to have a single leading slash always
- ensuring there are no unnecessary slashes.

The template just has to have a single construct `{{ 'css/style.css' | relative_url }}` and the links would be proper `(/blog/css/style.css)` for all the scenarios I mentioned above (`'/blog/'` or `'blog/'` or `'/blog//`')

---

<div class="post-metadata">

**Author:** ![yashumittal](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/yashumittal/32/1260_2.png) [@yashumittal](https://talk.jekyllrb.com/u/yashumittal)\
**Post date:** [August 16, 2018, 10:41am UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/7 "2018-08-16T10:41:43Z")

</div>

So, if I don’t use both baseurl and relative url, will it also work then?

---

<div class="post-metadata">

**Author:** ![ashmaroli](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@ashmaroli](https://talk.jekyllrb.com/u/ashmaroli)\
**Post date:** [August 16, 2018, 4:12pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/8 "2018-08-16T16:12:30Z")

</div>

> [@yashumittal](#):
>
> if I don’t use both baseurl and relative url, will it also work then?

It should work if you have hard-coded the links properly.  
Do you have a link to your site deployed to Gitlab Pages…?

---

<div class="post-metadata">

**Author:** ![yashumittal](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/yashumittal/32/1260_2.png) [@yashumittal](https://talk.jekyllrb.com/u/yashumittal)\
**Post date:** [August 17, 2018, 7:52am UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/9 "2018-08-17T07:52:04Z")

</div>

> [@ashmaroli](#):
>
> Do you have a link to your site deployed to Gitlab Pages…?

Here’s the link: [LogChimp Website](https://mittalyashu.gitlab.io/logchimp-website/)

---

<div class="post-metadata">

**Author:** ![jeff.milton](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/jeff.milton/32/1922_2.png) [@jeff.milton](https://talk.jekyllrb.com/u/jeff.milton)\
**Post date:** [September 28, 2019, 12:19pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/11 "2019-09-28T12:19:48Z")

</div>

> [@ashmaroli](#):
>
> {{ site.baseurl }}

I ran across this thread trying to solve a similar problem. I don’t have a github site yet, just doing everything locally, and my baseurl = “/mish”, so my URLs are `http://localhost:4000/mish/`, but I do not want a baseurl, or rather, I want the baseurl to be “/”, but then the pages can’t find any images, css, etc, which I do not understand because the server is actually running in the “/mish” directory, so the actual baseurl IS “/”.

Is the baseurl allowed to be just “/” or even “” ?

So, with baseurl= “/mish”, if I have the link:

` [here]({% post_url /knowledge/2018-01-29-KN-337 %})`

is breaks because it is missing the baseurl.

If I use

[here]({{ site.baseurl }}{% post\_url /knowledge/2018-01-29-KN-337 %})

It works, but if I set baseurl=“/”, everything breaks.

What am I missing?

---

<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:** [September 28, 2019, 3:34pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/12 "2019-09-28T15:34:44Z")

</div>

you probably should have it empty `baseurl:` or just delete baseurl from the config.

if you set it to ‘/’ and go look at what the url ends up with you will see `//` which will break since you already have a leading / on your url.

---

<div class="post-metadata">

**Author:** ![ijv](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/ijv/32/2532_2.png) [@ijv](https://talk.jekyllrb.com/u/ijv)\
**Post date:** [May 23, 2020, 8:34pm UTC](https://talk.jekyllrb.com/t/relative-url-and-baseurl/2051/13 "2020-05-23T20:34:07Z")

</div>

In v4.0.0 `post_url` tag now incorporates the `relative_url` filter within itself and therefore automatically prepends site’s `baseurl` to the post’s `url` value. If I have to remove all `{{ site.baseurl }}` based on this new upgrade…

What should I do in `main.scss` file with `$baseurl` variable? Remove it?  
(in Jekyll v3.x was `$baseurl: "{{ site.baseurl }}";`)
