# How to set up language auto-redirecting

**URL:** <https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768>\
**Category:** Questions\
**Tags:** v3\
**Created:** [January 12, 2021, 6:59pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768 "2021-01-12T18:59:01Z")\
**Posts on this page:** 12\
**Page:** 2

<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:** [January 12, 2021, 7:54pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/21 "2021-01-12T19:54:04Z")

</div>

The best option would be in a catch-all route, which can be put into the `/site/config/config.php` file or into a plugin.

---

<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:** [January 12, 2021, 7:55pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/22 "2021-01-12T19:55:36Z")

</div>

The alternative would be to rely on auto-detection

```auto
 'languages.detect' => true

```

(This would go into the return array in the config as well)

The result will then depended on the order of languages set by the user in the browser.

---

<div class="post-metadata">

**Author:** ![yannickmb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/yannickmb/32/9636_2.png) [@yannickmb](https://forum.getkirby.com/u/yannickmb)\
**Post date:** [January 12, 2021, 7:59pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/23 "2021-01-12T19:59:49Z")

</div>

Thanks again for your help 🙂 Let me share you the spot with language.detect:

```auto
// language detection on the home page with / as URL
                if ($home && $kirby->url() !== $home->url()) {
                    if ($kirby->option('languages.detect') === true) {
                        return $kirby
                            ->response()
                            ->redirect($kirby->detectedLanguage()->url());
                    } else {
                        return $kirby
                            ->response()
                            ->redirect($kirby->site()->url());
                    }

```

And the spot with configure.php:

```auto
<?php
return [
  'languages' => true,
  'languages.detect' => true,
  'debug' => false,
];

```

How could I add the condition to open the Spanish version with browsers set up in cat, basque or galician?

---

<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:** [January 12, 2021, 8:12pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/24 "2021-01-12T20:12:14Z")

</div>

If you have automatic language detection set in the config, you don’t need anything else.

---

<div class="post-metadata">

**Author:** ![yannickmb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/yannickmb/32/9636_2.png) [@yannickmb](https://forum.getkirby.com/u/yannickmb)\
**Post date:** [January 12, 2021, 8:15pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/25 "2021-01-12T20:15:47Z")

</div>

If you want to redirect to one URL (in this case /es) for more than one language, how do you do that? Now it only opens /es for Spanish

---

<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:** [January 12, 2021, 8:23pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/26 "2021-01-12T20:23:08Z")

</div>

I’m sorry, somehow I don’t seem to understand the problem quite correctly. Let me try and explain what I understand.

You have two languages, Spanish and English.

And you want that visitors from all over Spain and probably Spanish speaking countries overseas land on the Spanish language version, i.e. `yourdomain.com/es` and the rest of world end up on the English URL `yourdomain.com/en`. Right?

With language.autodetect set, this should actually happen based on this information (from the source code)

```auto
 $this->ip($arguments['ip'] ?? $_SERVER['REMOTE_ADDR'] ?? '');
        $this->userAgent($arguments['userAgent'] ?? $_SERVER['HTTP_USER_AGENT'] ?? '');
        $this->acceptedLanguage($arguments['acceptedLanguage'] ?? $_SERVER['HTTP_ACCEPT_LANGUAGE'] ?? '');
        $this->acceptedMimeType($arguments['acceptedMimeType'] ?? $_SERVER['HTTP_ACCEPT'] ?? '');

```

So if all goes well and you set your browser language to English and clear all cookies, you should end up on the English language version _when you visit the home page_.

---

<div class="post-metadata">

**Author:** ![yannickmb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/yannickmb/32/9636_2.png) [@yannickmb](https://forum.getkirby.com/u/yannickmb)\
**Post date:** [January 12, 2021, 8:47pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/27 "2021-01-12T20:47:35Z")

</div>

Everything you said is correct with the exception that catalan, galizian and basque are different languages. We want to charge the url [mydomain.com/es](http://mydomain.com/es) also for these three languages, instead of the /en, which is what happens now.

---

<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:** [January 12, 2021, 8:51pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/28 "2021-01-12T20:51:47Z")

</div>

> [@yannickmb](#):
>
> We want to charge the url [mydomain.com/es](http://mydomain.com/es) also for these three languages,

I know that these are all different languages. But what does that mean “charge the URL “es” for all three languages”? In my understanding that means that this URL and the language Spanish is presented to all Galician etc. speaking visitors.

---

<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:** [January 12, 2021, 8:55pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/29 "2021-01-12T20:55:53Z")

</div>

> [@yannickmb](#):
>
> , instead of the /en, which is what happens now.

But how do you test this? What is your browser language and region set to?

If automatic browser detection does not work, you need to set up a route with your custom logic.

```auto
  'routes' => [
    [
      'pattern' => '(:all)',
      'action' => function () {
        // do something here
        // when the URL matches the pattern above
      }
    ],
  ]

```

---

<div class="post-metadata">

**Author:** ![yannickmb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/yannickmb/32/9636_2.png) [@yannickmb](https://forum.getkirby.com/u/yannickmb)\
**Post date:** [January 12, 2021, 9:40pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/30 "2021-01-12T21:40:32Z")

</div>

The automatic language detection is working fine. The way the web is now set up is that if you have Spanish set in the browser, it opens the /es page. For any other language, it opens the /en site. What we want to change is, that when the browser has catalan, galizian or basque, it also opens the /es page.

So the logic function is:  
If language.detected = ‘es’ or ‘cat’ or ‘eu’ or ‘gal’  
Then open /es  
Else open /en

My question is, which is the code I have to use and where must it be placed

---

<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:** [January 12, 2021, 9:56pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/31 "2021-01-12T21:56:54Z")

</div>

Try this (not tested):

```auto
 'routes' => [
      [
          'pattern' => '(:all)',
          'action' => function () {
              $kirby= kirby();

                if ( option('languages.detect') === true && in_array( $kirby->detectedLanguage()->code(), ['es', 'eu', 'ca', 'gl']) ) {
                
                  
                  return $kirby
                      ->response()
                      ->redirect(kirby()->url('es');
                }

                $this->next();
            }
        ]
    ]

```

This would go into the return array of the config

---

<div class="post-metadata">

**Author:** ![yannickmb](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/yannickmb/32/9636_2.png) [@yannickmb](https://forum.getkirby.com/u/yannickmb)\
**Post date:** [January 21, 2021, 6:24pm UTC](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768/32 "2021-01-21T18:24:01Z")

</div>

Hi there, your proposal worked fine! Thanks a lot. The exact code I’ve used is this one (I’ve integrated your function with the code I already had):  
‘’’  
// language detection on the home page with / as URL

```
            if ($home && $kirby->url() !== $home->url()) {

                if ($kirby->option('languages.detect') === true && in_array( $kirby->detectedLanguage()->code(), ['es','eu','ca','gl'])) {

                    return $kirby

                        ->response()

                        ->redirect(kirby()->url('es'));

                } else {

                    return $kirby

                        ->response()

                        ->redirect($kirby->site()->url());

                }

```

‘’’  
Thanks for your support!

[Previous page](https://forum.getkirby.com/t/how-to-set-up-language-auto-redirecting/20768.md?page=1)
