asbjorn Posted December 14, 2020 Share Posted December 14, 2020 I have the following setup: Pages with two level URL segments, e.g. /this-page/urlsegment1/urlsegment2/ More than often, due to changes in the data set, the part of the url that is /this-page/ changes name to /this-page-something-else/ PagePathHistory (the modules is installed) saves previous names and handles all the redirects when it comes to the URL without URL segments. URL segmented pages returns 404 errors though Looking for a solution, I was thinking of using the information in the PagePathHistory like this: First, look for /this-page/urlsegment1/urlsegment2/, if it gives an 404 error: Second, look for /this-page/ in any pages PagePathHistory, if it exists: Third: Redirect to /this-page/'s new URL (/this-page-something-else/) I have tried: $pages->get("path=/this-page/") without luck (expecting no luck here) $pagePathHistory->getPage("/this-page/"), but this returns Fatal feil: Uncaught Error: Call to a member function getPage() Has anyone tried anything similar, with PagePathHistory, or maybe any third party modules? Link to comment Share on other sites More sharing options...
Robin S Posted December 14, 2020 Share Posted December 14, 2020 FYI, there is an open issue on this topic: https://github.com/processwire/processwire-issues/issues/1116 Here is a hook you can try in /site/init.php: $wire->addHookAfter('ProcessPageView::pageNotFound', function(HookEvent $event) { $url = $event->arguments(1); $pages = $event->wire()->pages; // Explode the URL to pieces, with an upper limit of 10 $pieces = explode('/', trim($url, '/'), 10); $redirect_page = $pages->newNullPage(); $segments = []; // Find closest real page, utilising PagePathHistory while($pieces && !$redirect_page->id) { $segments[] = array_pop($pieces); $path = '/' . implode('/', $pieces) . '/'; $redirect_page = $pages->getByPath($path, ['useHistory' => true]); } // If a page is found and the page's template allows URL segments // then redirect to it, adding back the potential URL segments if($redirect_page->id && $redirect_page->template->urlSegments) { $redirect_path = $redirect_page->url . implode('/', $segments) . '/'; $event->wire()->session->redirect($redirect_path); } }); 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now