Jump to content

Redirect to default version of site when the current page is not visible in specific language


Orkun
 Share

Recommended Posts

Hi Guys

I don't know what I am doing wrong. I have a website with three languages. German (default), French and English. The homepage should be only visible on the default language.

So i removed the check on french and english:

766475206_Bildschirmfoto2018-09-12um11_52_23.thumb.png.50f9252cc568eb368ff599a335782657.png

 

But some children pages of home are active in the french and english language. Let's say I am on a children page in the french language, for example www.example.net/fr/lorem/. When I click on the Logo (linked to home) it redirects as expected to www.example.net/fr/ and outputs the content of 404 Error Page. What can I do, so that it redirects to the default version of home and not to a 404 Error site.

 

I pasted this code to my home template but it only works when I am logged in: 

if(!$page->viewable($user->language)){
    $session->redirect($page->localUrl($languages->getDefault()));
}

 

I have also tried to catch it with a Hook on ProcessPageView::pageNotFound:

// hook on 404 page to render custom content or redirect
$this->addHookAfter("ProcessPageView::pageNotFound", $this, "hookPageNotFound");


protected function hookPageNotFound(HookEvent $event) {

        $url = trim($event->arguments("url"),"/");
        $page = $event->arguments("page");

        // it seems when page is unpublished, we have no "url" from page not found hook
        if($page->is("unpublished")){
            $url = $page->url;
        }

        // shortcut urls catch, if a page with "shortcut" matches requested url
        // we redirect to that page, sinmple and easy
        if($url) {
        	wire('log')->save("test", $url);
        }

    }

But www.example.net/fr/ doesn't trigger the hook...

 

What I am doing wrong?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...