Jump to content

Redirect with hooks in ready.php


SwimToWin
 Share

Recommended Posts

Hi, I want to redirect users that hit 1) pages using a specified template 2) when the url is using a specific syntax.

  • Url syntax is: /PRODUCT/redir/PAGEID
  • Example: /foo/redir/1234
  • My intention is to redirect the user to the specified PAGEID - but I don't know how to user $input in a hook.

How might that be done with hooks in ready.php?

$this->addHookBefore('Session::redirect', function(HookEvent $event) {
    $session = $event->object;

    // (Product template always lives a root level)
    if( TEMPLATE == 'product' && INPUT->urlSegment1()=='r' ) {
        $dest = $pages->get(PAGEID);
        $session->redirect( $dest->url );
    }
});
Link to comment
Share on other sites

Good idea, @Markus Thomas.

Unfortunately these url hooks don't work in my use case because the redirect happens "below existing pages", as explained here:

$wire->addHook('/existing-page/r', function($event) {
    return "Hello R"; // Not shown (contents from the "nearest parent page" is shown)
}); 

$wire->addHook('/non-existing page/r', function($event) {
    return "Hello"; // Works
}); 
Link to comment
Share on other sites

This work in my case whithout any problems

$wire->addHook('/existing-page/([0-9]+)', function ($event) {
    $id = $event->arguments(1);
    $post = $event->pages->findOne("template=product, id=$id");
    if ($post->viewable()) $event->session->redirect($post->url);
});

 

Link to comment
Share on other sites

Hi @Markus Thomas - Thank you very much for pointing out the opportunity to use url hooks. I can make it work on one page, but it keeps failing on the product page and I cannot tell why; I'm using PW v3.0.200. Anyways - using urlSegments() works: #workaround #uglyButWorks #inspiration

Code

// Match url - example: https://www.example.com/myproduct/r/foo1234
if( in_array($page->template, ['product','redirect']) && $input->urlSegment1()=='r' && $input->urlSegment2() ) {
    $p = $pages->findOne("template=redirect, title=" . $input->urlSegment2() );

    if( $p->link ) {
        // Page exists according to "page that uses a redirect template" -> redirect to specified destination page.

        // Support absolute and relative links
        $p->link = str_replace(['https://www.example.com/','http://www.example.com/','//www.example.com/','www.example.com'], '', $p->link);
        $p->link = ltrim($p->link,'/');
        $url = "//{$config->httpHost}/$p->link";
    } else {
        // Fallback: Page doesn't exist, so redirect to product page
        $url = "//{$config->httpHost}/{$page->url}";
    }

    printf("<p>Redirecting to <a href='%s'>%s</a></p>", $url, $url);
    $session->redirect($url);
}

Setup

Redirect template with Title and Link text fields.

 

 

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

×
×
  • Create New...