Leftfield Posted December 8, 2018 Share Posted December 8, 2018 Hi All, I am newbe to PW (was in Joomla for more then 10 years)... I got this kind of URLs: www.name.com/posts/article www.name.com/categories/nameofcategory 1. Is there some natural way I can rewrite URL so it will be (except arranging backend in this order) www.name.com/nameofcategory/article www.name.com/nameofcategory 2. If rewriting, will it (significantly) impact speed? Link to comment Share on other sites More sharing options...
dragan Posted December 8, 2018 Share Posted December 8, 2018 What does your current page (page-tree) structure look like? Link to comment Share on other sites More sharing options...
Zeka Posted December 8, 2018 Share Posted December 8, 2018 @Leftfield Welcome to the community. The easiest and most performant way is just to make page tree reflects your desirable URL structure. But often such structure looks messy. You can change it by using URL segments and rewriting page paths for your category and post templates. For example, for blogs, I use such a structure home/blog/ /authors /categories /cat1 /cat2 /posts /post1 /post2 I got such URLs site.com/blog/category/cat1 site.com/blog/posts/post1 etc. But I want to have such URLs and I want that every post can be listed in several categories, but for preventing contend duplication I want that it has only one accessible URL site.com/cat1/ site.com/cat1/post1/ site.com/cat2/post2/ To make it work you have to: 1. Enable URLs segments for your home page. 2. Create to Page Reference fields and add it to your post template blog_section - Page reference field ( Page field value type set to Single page, selector 'template=blog_category') - this one we will use for building URL for the current post. blog_categories - Page reference field ( Page field value type set to Multiple pages, selector 'template=blog_category') 2. In your ready.php $pages->addHookAfter('Page(template=blog-category)::path', function ($e) { $page = $e->object; $e->replace = true; $e->return = "/" . $page->name . "/"; }); $pages->addHookAfter('Page(template=post)::path', function ($e) { $page = $e->object; if ($page->blog_section) { $slug = $page->blog_section->name; } else { $slug = $page->parent('template=blog')->name; //fallback } $e->replace = true; $e->return = "/" . $slug . "/" . $page->name . "/"; }); 3. In your home.php if ($input->urlSegment3) throw new Wire404Exception(); if ($input->urlSegment2) { $post_name = $input->urlSegment2; $match = $pages->get($sanitizer->selectorValue($post_name)); if (!$match->id) throw new Wire404Exception(); echo $match->render(); return $this->halt(); } if ($input->urlSegment1) { $category_name = $input->urlSegment1; $match = $pages->get($sanitizer->selectorValue($category_name)); if (!$match->id) throw new Wire404Exception(); echo $match->render(); return $this->halt(); } It's not tested and there can be some errors, but I hope you get a general idea. 4 1 Link to comment Share on other sites More sharing options...
Leftfield Posted December 8, 2018 Author Share Posted December 8, 2018 12 hours ago, dragan said: What does your current page (page-tree) structure look like? home/blog/ /authors /categories /cat1 /cat2 /posts /post1 /post2 Got it from @Zeka. I will finish it. The other thing was: Does it slow down? Does it reflect on the speed? Link to comment Share on other sites More sharing options...
Zeka Posted December 8, 2018 Share Posted December 8, 2018 @Leftfield There is answer from Ryan 2 Link to comment Share on other sites More sharing options...
Zeka Posted December 8, 2018 Share Posted December 8, 2018 Also it worth to take a look at this thread 1 Link to comment Share on other sites More sharing options...
Leftfield Posted December 9, 2018 Author Share Posted December 9, 2018 Yes, I read everything. 1 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