<p>You include the language prefix in each site's <a href="https://craftcms.com/docs/3.x/sites.html#site-url" rel="nofollow noreferrer">URL setting as per the documentation</a>. It's fine that URIs of entries don't include the site URL, this way they stay language-independent. You wouldn't want to have to update all URIs if you need to change the base URL of one site.</p>
<p>If you create site URLs as intended, you will never notice that the language prefix / base URL is added on the fly:</p>
<ul>
<li>For elements: Use <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#method-geturl" rel="nofollow noreferrer"><code>Element::getUrl</code></a> (e.g. <code>entry.getUrl()</code> in Twig)</li>
<li>For custom links: Use the <a href="https://craftcms.com/docs/3.x/dev/functions.html#siteurl" rel="nofollow noreferrer">siteUrl</a> function.</li>
</ul>

CC BY-SA 4.0
2022-02-03T08:15:27.257Z
Thanks for the response. I'm using Craft in headless mode and with a client-side router which needs the URI. So I would either have to remove the host from each URL or add the language in front of every URI, both of which seem like hacks.
Thomas
CC BY-SA 4.0
2022-02-03T09:10:39.650Z
@Thomas Hm, hard to say in this case, depends on your client-side routing setup. In this case, do you need language prefixes on the server-side at all? You probably already have language prefixes set up in your client-side routing, so I'd pass along the site ID or handle for each entry through the API and build URLs in the frontend based on that. Of course you need to maintain a mapping between site IDs/handles to languages, but that shouldn't be a problem.
MoritzLost
CC BY-SA 4.0
2022-02-02T15:06:12.010Z
2022-02-02T15:06:12.010Z
MoritzLost
<p>I never used Craft before and I'm trying to create a multi-language site (in headless mode) but am running into problems with adding the language to the URL.</p>
<p>My goal is to have URLs that always start with the language, e.g. <code>/en/page</code> and <code>/de/seite</code>. I started by adding that prefix to the base URL of each language site which works fine for querying URLs but not for URIs. I could manually prepend the language to each link but that seems wrong...</p>
<p>What is the recommended way of adding such a language prefix?</p>
<ul>
<li>Adding the prefix to each language site (manually adding the prefix to each link)</li>
<li>Adding the prefix to every section (manually adding the prefix for each section).</li>
</ul>

<localization><language><graphql><headless>
Language prefix in URL with correct entry URIs
# Language prefix in URL with correct entry URIs

(Asked by: username_0 on 2022-02-02)

I never used Craft before and I'm trying to create a multi-language site (in headless mode) but am running into problems with adding the language to the URL.

My goal is to have URLs that always start with the language, e.g. `/en/page` and `/de/seite`. I started by adding that prefix to the base URL of each language site which works fine for querying URLs but not for URIs. I could manually prepend the language to each link but that seems wrong...

What is the recommended way of adding such a language prefix?

* Adding the prefix to each language site (manually adding the prefix to each link)
* Adding the prefix to every section (manually adding the prefix for each section).

**Tags:** localization, language, graphql, headless

---

## 1 answers

### Answer by username_1 on 2022-02-02. Score: 1 | Accepted answer

You include the language prefix in each site's [URL setting as per the documentation](https://craftcms.com/docs/3.x/sites.html#site-url). It's fine that URIs of entries don't include the site URL, this way they stay language-independent. You wouldn't want to have to update all URIs if you need to change the base URL of one site.

If you create site URLs as intended, you will never notice that the language prefix / base URL is added on the fly:

* For elements: Use [`Element::getUrl`](https://docs.craftcms.com/api/v3/craft-base-element.html#method-geturl) (e.g. `entry.getUrl()` in Twig)
* For custom links: Use the [siteUrl](https://craftcms.com/docs/3.x/dev/functions.html#siteurl) function.

> **Comments:**
> **username_0**: Thanks for the response. I'm using Craft in headless mode and with a client-side router which needs the URI. So I would either have to remove the host from each URL or add the language in front of every URI, both of which seem like hacks.  
> **username_1**: @Thomas Hm, hard to say in this case, depends on your client-side routing setup. In this case, do you need language prefixes on the server-side at all? You probably already have language prefixes set up in your client-side routing, so I'd pass along the site ID or handle for each entry through the API and build URLs in the frontend based on that. Of course you need to maintain a mapping between site IDs/handles to languages, but that shouldn't be a problem.  

---

