SEO friendly localised urls |
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']); 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?
Sounds like it is now updating the html, I do it like this using the Session.
PHP Code: /** 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: /** What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
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']); 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?
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 )
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'
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 )
@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> |
Welcome Guest, Not a member yet? Register Sign In |