Jump to content

[SOLVED] URL rewriting - removing first cat and mix


Leftfield
 Share

Recommended Posts

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

@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. 

  • Like 4
  • Thanks 1
Link to comment
Share on other sites

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

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...