Remove 'home' in url on multi language site


#1

Similar to this thread:
Home subpages (remove home in the URL)

I’m removing ‘home’ from the url because I’m using subpages in the home folder. But I’m using this on a multi language site (nl which is default and en). Could somebody show me what the page model and route for my multi language situation should be? Much appreciated, thank you kindly!

Code from the above mentioned thread:

    c::set('routes', array(
	array(
		'pattern' => '(:any)',
		'action'  => function($uid) {
			$page = page($uid);

			if(!$page) $page = page('home/' . $uid);
			if(!$page) $page = site()->errorPage();

			return site()->visit($page);
		}
	),
	array(
		'pattern' => 'home/(:any)',
		'action'  => function($uid) {
			go($uid);
		}
	)
));

<?php
class ProjectPage extends Page {
  public function url() {
    return $this->site()->url() . '/' . $this->uid();
  }
}

#2

Either duplicate your routes for each language or try @lukaskleinschmidt’s suggestion with the regex for the language code: Routes pattern for multilanguage site


#3

Thanks, but can’t get my head around it it’s beyond my skill level I’m afraid. Could you post an example or is that to much to ask? That’s fine, in that case I might just use a folder outside home and call it something generic like articles.


#4

This should work:

c::set('routes', [
  [
  'pattern' => '(?:(^[A-Za-z]{2})//?)?(:any)',
  'action'  => function($lang, $uid) {
    $lang = $lang ? : 'en'; // the default language
    if($uid == 'de') /*non-default language */ {
      return site()->visit(page('home'), 'de');exit;
    }
    $page = page($uid);

	if(!$page) $page = page('home/' . $uid);
	if(!$page) $page = site()->errorPage();

	return site()->visit($page, $lang);
  }
],
 [
  'pattern' => '(?:(^[A-Za-z]{2})//?)?home/(:any)',
  'action'  => function($lang, $uid) {
    $lang = $lang ?: 'en'; // the default language

	  go($lang.'/'.$uid);
  }
]
]);

replace languages with what applies to your use case.


#5

Thank you,
I tried this but this causes problems with the language switcher and shows nl (default) in the url also.
I’m switching to another solution; no subfolder in the home folder. Which makes live easier for me (and for you ;-). Thanks for your quick reply though, very much appreciated!


#6

Looks like I made a mistake in the second route, in my starter kit it works now without the lang code in the default language.

[
  'pattern' => '(?:(^[A-Za-z]{2})//?)?home/(:any)',
  'action'  => function($lang, $uid) {
    $lang = $lang ?: ''; // the default language

	  go($lang.'/'.$uid);
  }
]

#7

Yes, that works now but:
On the homepage I can switch from en to nl, but I can only reach the nl articles. The en articles from home and from the nl article page throws an error.


#8

Not sure I completely understand. I would need more information about your exact structure I think. Multi-lang routing without the language code in the default language is always a chore.


#9

Don’t worry you’ve been a great help, I think we can close this and I’ll carry on with the alternative approach.