# How to refer to internal pages via ID instead of URLs?

**URL:** <https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996>\
**Category:** Help\
**Created:** [February 28, 2024, 12:08pm UTC](https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996 "2024-02-28T12:08:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VillyVretthorn](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@VillyVretthorn](https://talk.jekyllrb.com/u/VillyVretthorn)\
**Post date:** [February 28, 2024, 12:08pm UTC](https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996/1 "2024-02-28T12:08:28Z")

</div>

Hi,

I often link to various internal pages including blog posts, etc.

I would like to be able to make a link to a page using some sort of unique page ID instead of the actual URL.  
For example, lets say I have a page defining some concept foo. I would then like to be able to link with something like this `[Foo](pages.foo)` instead of `[Foo](/en/products/category/foo.html)`  
In the latter case with the URL, I will have to go through all pages to update references in the event that I move the URL from `/en/products/category/foo.html` to, say, `/en/products/foo.html`.  
I could probably make something work by giving each page (or maybe even just specific pages I link to often) a variable `id` in their preamble and then find some way to look this up using liquid… But I don’t want to reinvent the wheel (badly) if it has already been invented. I think a desire for this functionality would be fairly common.

---

<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 28, 2024, 4:43pm UTC](https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996/2 "2024-02-28T16:43:28Z")

</div>

there are built in link and post urls you can do that sound close to what you want, though if you move or rename a file I am not sure they will work. They are more about taking into consideration the permalink and baseurl as I understand it.

> **[URLs and links in Jekyll](https://mademistakes.com/mastering-jekyll/how-to-link/#linking-posts)**
>
> How Jekyll uses URLs and how to link posts, pages, assets, and other resources together.

---

<div class="post-metadata">

**Author:** ![VillyVretthorn](https://avatars.discourse-cdn.com/v4/letter/v/a587f6/32.png) [@VillyVretthorn](https://talk.jekyllrb.com/u/VillyVretthorn)\
**Post date:** [March 4, 2024, 8:53am UTC](https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996/3 "2024-03-04T08:53:31Z")

</div>

I think the link tag ([Tags Filters | Jekyll • Simple, blog-aware, static sites](https://jekyllrb.com/docs/liquid/tags/#link)) is close to what I want. However not quite. Link seems to be able to translate the local path into an URL. That is all well. However, I want to use something short that is mentioned in the frontmatter as identifier.

So for example, with link I could use  
`{% link _posts/2016-07-26-name-of-post.md %}` or `{% link news/index.html %}` to generate `/2016/07/26/name-of-post.html` or `/news/` respectively.

What I need is abstracted one level above this. So I want to be able to go into 2016-07-26-name-of-post.md and news/index.html and put in front matter like: `id: namepost` and `id: news` and then from anywhere in jekyll be able to do something like:  
`{% url "namepost" %}` or `{% url "news" %}` and have those generate `/2016/07/26/name-of-post.html` or `/news/` respectively.

I hope this makes sense.

There are obvious use cases for this. One is if I have a term that is described on a specific page and I use this term a lot. Maybe I want to link the first occurrence of this term on each page to where that term is described.  
Now, when I start out, maybe the description of this term is a blog entry. Maybe later I decide to make a dedicated (non-blog) page that describes this term. Without the functioanlity I describe above, I would have to go into all pages linking to this page and change all the links. This is solved by what I describe.

It might also be possible that I could hack something up using \_data files. Maybe I could have a \_data/urlalias.yml and then have a list in there that maps ids to URLs. Then I would just have to change the mapping one place if I rename or move a file. However, I am not clever enough to actually make this work…  
I think the \_data approach might be the way to go, because that way I could probably also use it for all other kinds of assets. For example if I have some icon that I use all the time, I could easily link to it using `<img src="{% url successicon %}>` instead of `<img src="/assets/images/icons/32x32/success-32x32-optimized.png">`.

If I then later want to change that icon into a .webp image or simply use another URL, I could just change this in the \_data 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:** [March 4, 2024, 5:11pm UTC](https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996/4 "2024-03-04T17:11:56Z")

</div>

page.url will get you the url to the page if you loop thru and find the page. So you could do front matter and have an id = 1234 and then do an if statement to find a page where the id = 1234 and then get the url of that page.

But that won’t scale well if you have a lot of pages and I think it would be difficult to manage.

Your idea of using a data file may be better, it shouldn’t be difficult to do though I am not sure it will perform any better - but at least everything would be in one place to edit.

this site has a lot of example of data stuff:

> <https://github.com/planetjekyll/quickrefs/blob/master/YAML.md>

---

<div class="post-metadata">

**Author:** ![george-gca](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.jekyllrb.com/george-gca/32/5301_2.png) [@george-gca](https://talk.jekyllrb.com/u/george-gca)\
**Post date:** [March 6, 2024, 3:44pm UTC](https://talk.jekyllrb.com/t/how-to-refer-to-internal-pages-via-id-instead-of-urls/8996/5 "2024-03-06T15:44:48Z")

</div>

Not sure how this is done, but the [polyglot plugin](https://github.com/untra/polyglot?tab=readme-ov-file#using-different-permalinks-per-language) has something like this inside, so it can find which page is the equivalent to that same page in a different language.
