Jump to content
joer80

Check alternate path before throw 404

Recommended Posts

I have a page located at /pages/about-us/.  Is it possible to write code that looks for it at /about-us/ before I throw the 404?

I would like all children of pages to check this way.  Not just this one.

Thanks!

Share this post


Link to post
Share on other sites

You can hook to pageNotFound

// in modules init()
$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'redirectToEnglish');

public function redirectToEnglish($event){
  // some logic
	$this->session->redirect($yourpage->url);
}

Share this post


Link to post
Share on other sites
1 hour ago, Zeka said:

You can hook to pageNotFound


// in modules init()
$this->addHookBefore('ProcessPageView::pageNotFound', $this, 'redirectToEnglish');

public function redirectToEnglish($event){
  // some logic
	$this->session->redirect($yourpage->url);
}

So what I want to happen, is if I visit /about-us/, it will load the /pages/about-us/ page as $page without the user seeing a redirect.  Is that what this would do? Or would they get redirected?

Share this post


Link to post
Share on other sites
23 minutes ago, joer80 said:

Or would they get redirected?

A $session->redirect does exactly what it says on the tin; it will redirect.

It seems you have a parent page called 'pages'. One of its children is 'about-us'. In the frontend, you don't want people to see 'pages' in the URL. You want them to access the parent's children directly. For instance, you want ONLY 'about-us' in the URL. Finally, you don't want a redirect Is this correct?

Share this post


Link to post
Share on other sites
Just now, kongondo said:

A $session->redirect does exactly what it says on the tin; it will redirect.

It seems you have a parent page called 'pages'. One of its children is 'about-us'. In the frontend, you don't want people to see 'pages' in the URL. You want them to access the parent's children directly. For instance, you want ONLY 'about-us' in the URL. Finally, you don't want a redirect Is this correct?

Correct.  Hard to explain! 😄

Share this post


Link to post
Share on other sites

In that case what you want is to rewrite the URL. It is a divisive topic :-). Have you considered alternative, e.g. restructuring your site? Anyway, if you are sure you want to rewrite the URL, this is the go to topic:

See the section on URLs.

This post might also be relevant:

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks @kongondo I think I have it.  Ryans post helped a ton!

This is not a live website yet so I am playing around with the tree layout, but I do see perks for this.  This is the tree:

Root

- Pages
- Posts
- Services
- Products
- Menus

So I wanted pages to be special in that for a page like /pages/about/, you could opt to use the short form /about/. 

This not only matches most websites you would migrate to processwire, but it is so clean and organized.

Also, I wanted you to be able to have 2 homepages, and edit the root page and select which page you want to load as your active homepage.  You could even make the blog or posts page your homepage with a click.

After looking at Ryans code, I ended up turning on page segments for root and doing the below code in my root.php.  
 

//Allow pages to sit under root instead of under pages area.
if($input->urlSegment(1)){
	//might be 404 or a url directly under root.
	$realPage = $pages->get('/pages/' . $input->urlSegment(1) . '/');
	
	if($realPage->id){
		$page = $realPage;
	} else {
		throw new Wire404Exception();
	}
	
} else {
	//matched a page, no need to check for short link.
	
	//Set the root variable and page variable. (If on homepage, use the selected homepage instead)
	if($page->id == 1){
        $root = $page;
        $page = $page->selected_homepage;
    } else {
        $root = $pages->get('/');
    }
}

I also fixed the page path like this in site/init.php

//If we are viewing the long path for a page, set the path value to the short version. (Pages live in /pages/)
$wire->addHookBefore('Page::path', function($event) {
	
	$page = $event->object;
	
	if($page->template == 'page') {
		
		$event->replace = true;
    	$event->return = "/$page->name/";
		
	}
	
});

 

Share this post


Link to post
Share on other sites
35 minutes ago, joer80 said:

Thanks @kongondo I think I have it.  Ryans post helped a ton!

Glad you got it sorted :-).

Share this post


Link to post
Share on other sites
On 4/23/2020 at 5:23 PM, joer80 said:

This is not a live website yet so I am playing around with the tree layout,

In that case then, in case you missed it, here's a new alternative way to do custom paths

 

Share this post


Link to post
Share on other sites
15 hours ago, kongondo said:

In that case then, in case you missed it, here's a new alternative way to do custom paths

 

Oh wow!  That is really neat.  I will keep that in mind.  

After taking the time to figure it all out, I am wondering now if in my situation it would just be better to keep the tree natural, and make a dashboard that you see by default after login, that groups the pages how I want them to be grouped.  lol

But this may be handy to keep in the mind in the future if I redo an existing website and dont want to do redirects and need that flexibility. 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...