# Route to redirect to error page (404) on multilanguage site

**URL:** <https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532>\
**Category:** Questions\
**Created:** [May 3, 2024, 10:00am UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532 "2024-05-03T10:00:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [May 3, 2024, 10:00am UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/1 "2024-05-03T10:00:55Z")

</div>

Hi there,

On my multilanguage website, when someone is visiting a page which isn’t published or doesn’t exist, I would like to redirect to the error page with the right translation using a route (so URL becomes /404 or /en/404). I’ve tried the following code but it doesn’t work…

Does someone know how to do this properly please? 🤔

```auto
[
  'pattern' => '(:any)',
  'language' => '*',
  'action' => function ($language, $uid) {
    $page = page($uid);
    if(!$page->isPublished() || !$page->exists()) go('error');
  }
],

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 3, 2024, 10:07am UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/2 "2024-05-03T10:07:25Z")

</div>

See: [Routes | Kirby CMS](https://getkirby.com/docs/guide/routing#multi-language-setup), the important part is `site()->visit()`, go() doesn’t have an option to pass a language code.

---

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [May 3, 2024, 11:44am UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/3 "2024-05-03T11:44:52Z")

</div>

Using _visit_ or _go_ leads me to the same following error when I type a random page URL in the address bar, so I may have missed something…

```auto
Call to a member function isPublished() on null

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 3, 2024, 11:46am UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/4 "2024-05-03T11:46:33Z")

</div>

> [@tacotac](#):
>
> ` $page = page($uid);`

Well, you don’t check for a page object before calling `isPublished()` if you have a page object. the same is true for `exists()`

So you would need:

```auto
  $page = page($uid);
    if(!$page || !$page->isPublished()) go('error');

```

Also note that the `(:any)` placeholder only catches first level pages.

---

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [May 3, 2024, 3:39pm UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/5 "2024-05-03T15:39:03Z")

</div>

Thanks for your help Sonja 🙂  
I’ve got this right now…

```auto
[
  'pattern' => '(:any)',
  'language' => '*',
  'action' => function ($language, $uid) {
    $page = page($uid);
    if(!$page || !$page->isPublished()) go('error');
    else return site()->visit($page, $language);
  }
]

```

… however I cannot preview the draft pages when logged in anymore. Is there any workaround using something like _!kirby()-\>user()_ ?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 3, 2024, 3:45pm UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/6 "2024-05-03T15:45:21Z")

</div>

I’m wondering what you are trying to achieve here? On a multi-language site, don’t you automatically get the correct error page when a page doesn’t exist? at least that should be the case…

But yes, you can of course check if a user is logged in

```auto
if (!$page || (!kirby->user() && $page->isPublished()) { 
// do stuff
}

```

---

<div class="post-metadata">

**Author:** ![tacotac](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tacotac/32/14065_2.png) [@tacotac](https://forum.getkirby.com/u/tacotac)\
**Post date:** [May 3, 2024, 4:46pm UTC](https://forum.getkirby.com/t/route-to-redirect-to-error-page-404-on-multilanguage-site/31532/7 "2024-05-03T16:46:30Z")

</div>

I’m using the Swup JS library to play with the history API and I need error pages to redirect to a specific URL in order to animate correctly transitions, so this is very specific. I’ll try to find an alternative and will share here the solution.

Thanks for your time Sonja 😉
