a-ok Posted August 24, 2016 Share Posted August 24, 2016 I have a list of articles, all under the parent 'Articles', however I would like the URL for each to be, for example, www.website.com/this-is-an-article rather than www.website.com/articles/this-is-an-article. Any ideas if this is possible? Link to comment Share on other sites More sharing options...
adrian Posted August 24, 2016 Share Posted August 24, 2016 Lots of discussion, links and approaches in this thread: 4 Link to comment Share on other sites More sharing options...
a-ok Posted August 24, 2016 Author Share Posted August 24, 2016 Thanks adrian. Do you happen to know if this works in 3+? I have the following in ready.php which seems to re-route the URLs but page itself states 404? <?php wire()->addHookBefore('Page::path', function($event) { $page = $event->object; if ($page->template == 'article-detail') { $event->replace = true; $event->return = "/$page->name/"; } }); Link to comment Share on other sites More sharing options...
Robin S Posted August 24, 2016 Share Posted August 24, 2016 45 minutes ago, oma said: I have the following in ready.php which seems to re-route the URLs but page itself states 404? Have you set up URL segments for the home page template? See Ryan's post where he talks about the home template: 4 Link to comment Share on other sites More sharing options...
deltavik Posted August 25, 2016 Share Posted August 25, 2016 I am on pw 3 and this is the code that works for me. I am new to PW and haven't yet caught up with hooks -- I got this code from somewhere here. <?php $pages->addHookAfter('Page::path', null, 'hookPagePath'); function hookPagePath(HookEvent $e) { $page = $e->object; if($page->get('template') == 'article') $e->return = "/$page->name/"; } This code that updates the urls is the first half of the redirection game. The other half is about redirecting users to the right article. For that, you will have to use urlsegments. Here are the steps, roughly. -- Enable urlsegments for homepage. => This means website.com/this-is-an-article is a hit to the homepage, and this-is-an-article is the first url segment. -- Use this url segment to get a handle to the article page object. ==> $pages->get("name=this-is-an-article") -- Check if its a legit article by checking for the existence of its id. -- If it's a valid page, redirect the visitor to it. echo $article->render(); return; 5 Link to comment Share on other sites More sharing options...
a-ok Posted August 25, 2016 Author Share Posted August 25, 2016 Thanks everyone! Link to comment Share on other sites More sharing options...
a-ok Posted October 24, 2018 Author Share Posted October 24, 2018 Sorry to bring this back up... over 2 years later... but I'm a little stuck. I'm wanting to run child pages (website.com/brands/this-is-a-brand/) from the root (website.com/this-is-a-brand/) but when using a hook or urlSegments I am getting nowhere with it. What's the best process to do this? Do I need to enable a urlSegment check on home.php and allow urlSegments? @Macrura included this and have added it to ready.php but I'm unsure of what's meant to happen? Any help? Link to comment Share on other sites More sharing options...
Macrura Posted October 24, 2018 Share Posted October 24, 2018 yes, you need to enable URL Segments on the homepage. then, if you do the hook, when you click "View Page" from the editor, it should go to the new URL off the root. So if you have a page that is posts/name-of-page, that would now be /name-of-page Link to comment Share on other sites More sharing options...
a-ok Posted October 25, 2018 Author Share Posted October 25, 2018 Thanks for this, @Macrura This is fine... and then on my home.php template I included the following at the top before anything else: if ($input->urlSegment1) { $name = $sanitizer->pageName($input->urlSegment1); $brand = $pages->get("/brands/")->child("name=$name"); if ($brand->id) { return $brand->render(); } else { throw new Wire404Exception(); } } This seemed to work... best way of doing it? Link to comment Share on other sites More sharing options...
Macrura Posted October 27, 2018 Share Posted October 27, 2018 looks correct ? 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