Welcome Guest, Not a member yet? Register   Sign In
SEO friendly localised urls
#1

Hi,
I am trying to improve my multilingual website, building on https://includebeer.com/en/blog/creating...r-4-part-1.
I would like to have my urls in the language of the page. For example, for a research page, have /en/research for english content and /fr/recherche for french. My routes are defined as such: 
Code:
$routes->get('/{locale}/research', 'PagesController::show/research', ['as' => 'research']);

I thought I could do this inside routes.php:
Code:
$routes->get('/en/research', 'PagesController::show/research', ['as' => 'enResearch']);
$routes->get('/fr/recherche', 'PagesController::show/research', ['as' => 'frResearch']);

And modify my menu to call for the right alias like that
Code:
<?= route_to($locale."Research") ?>

But then  I loose the locale, so if I am on the english version of the page and click on the link to view the page in French, I still see the english version (with the French url).
What would be the best approach to make SEO friendly urls in this case?
Reply
#2

(This post was last modified: 09-04-2024, 09:46 PM by InsiteFX.)

Sounds like it is now updating the html, I do it like this using the Session.
PHP Code:
/**
 * @var TYPE_NAME $locale
 * @var TYPE_NAME $supportedLocales
 * @var TYPE_NAME $page
 */
$currentLocale session()->get('locale');
?>
<!doctype html>
<html lang="<?= $currentLocale?>" data-bs-theme="auto"> // <-- this has to be updated when changing the language! 

I also have created a Bootstrap 5.3.3 NavBar Language Selection DropDown that handles all CodeIgniter 4 Translations.
I' am still working on it but so far everything is working.
The language is changed in the BaseController.

BaseController:

PHP Code:
      /**
        * E.g.: $this->session = \Config\Services::session();
        * Ensure that the session is started and running
        */
        if (session_status() == PHP_SESSION_NONE) {
            $this->session Services::session();
        }

        $config = new App();

        $this->viewData['page'] = '';
        $this->viewData['locale'] = $this->request->getLocale();
        $this->viewData['supportedLocales'] = $config->supportedLocales;

        session()->set('locale'$this->request->getLocale()); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
Reply
#4

Thanks for sharing your approach.
I can make the view work when I keep the {locale} placeholder inside my routes like that:
Code:
$routes->get('/{locale}/research', 'PagesController::show/research', ['as' => 'enResearch']);
$routes->get('/{locale}/recherche', 'PagesController::show/research', ['as' => 'frResearch']);

and then calling the correct alias inside my menu with the locale passed from the BaseController to the view 
Code:
<?= route_to($locale."Research") ?>

But it does not feel entirely clean? Or is it?
Reply
#5

Did some searching and found this on Google, watch the video also takes about a half hour read.

Tell Google about localized versions of your page
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

Hi,
thanks for your suggestion, I already watched those and am following google's requirements for the alternate urls.
My question is about the best coding approach to localise my urls and adjust the changing language menu in a way that works.
I am looking for feedback on the codeigniter part of things.
Is my approach to routes okay? If so, then I can move on to the next 'challenge' Smile
Reply
#7

Not sure if your codes are ok but I' m now questioning this part on Google.
Maybe @kenjis can clarify this for us.
Google says:
Supported language and region codes

The hreflang attribute's value is comprised of one or optionally two values, separated by a dash.
For example, en-US. The first code of the hreflang attribute is the language code (in ISO 639-1 format)
followed by an optional second code that represents the region code (in ISO 3166-1 Alpha 2 format)
of an alternate URL.

Warning: You can't specify the country code by itself. The first code stands for the language and Google
doesn't automatically derive the language from a country code.

@kenjis, Doe's this mean that Google is ignoring the 2 code language code?
Should we be using the 5 code? en-GB etc?

h
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#8

@InsiteFX

> Doe's this mean that Google is ignoring the 2 code language code?

No. 
Read it carefully.

> Warning: You can't specify the country code

You can't specify country code. You can specify language code.
You see hreflang="en" in the sample code.

Code:
<head>
<title>Widgets, Inc</title>
  <link rel="alternate" hreflang="en-gb"
      href="https://en-gb.example.com/page.html" />
  <link rel="alternate" hreflang="en-us"
      href="https://en-us.example.com/page.html" />
  <link rel="alternate" hreflang="en"
      href="https://en.example.com/page.html" />
  <link rel="alternate" hreflang="de"
      href="https://de.example.com/page.html" />
<link rel="alternate" hreflang="x-default"
      href="https://www.example.com/" />
</head>
Reply
#9

@kenjis, ok thanks for the clarification.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB