Jump to content

Strip parent from Url


Cesco
 Share

Recommended Posts

Hi

i need to insert many pages (over 300) to my website. This pages are buildings and i want to organize them like children inside a parent page.
Example:

buildings
    |_ building 1
    |_ building 2

....

So i want that url of this pages are not "https://.... /buildings/building-1" but "https:// .... /building-1"

I don't want to use rewrite because i think it not a good solution about SEO. Can i strip parent url before ?

Thanks

Francesco 

 

Link to comment
Share on other sites

Well, you’re going to have to fight ProcessWire at least a little bit to do something like this. PW makes it easy for you to make the pages accessible at example.com/building-1 by using UrlSegments or the new PathHooks, but the page will still have the path /buildings/building-1 internally, and every time you use $page->url or $page->httpUrl, you’ll get the “real” path (according to the page tree).

So it depends on how far you want to go. If you just want buildings to be accessible as though they were somewhere else in the page tree, while also being accessible at their “real” path, you just use one of the two techniques I linked above and you’re done. You still have to watch out for pages whose actual path collides with the “shortcuts”, but it’s probably not going to be a problem unless you have buildings called “contact-us” or something ?

If you want to completely hide the buildings’ real urls, it gets more complicated. You might throw Wire404Exceptions or simply have no template file associated with the building template (although that will lead to other problems, like I believe it will make $page->viewable always return false). Also may need to find a way to generate your “fake” paths and use them instead of the usual $page->url everywhere (e.g. for the canonical tag, sitemaps, every time you link to a building...).

If your buildings use UrlSegments or have children themselves, more problems arise.

It may be worthwhile to have all buildings be children of the home page and build a way to deal with the crowded page tree in the Admin instead.

Link to comment
Share on other sites

Thanks @Jan Romero for your answer. My goal is to have the url of buildings like children of home page, so $page->url must return "shortcut" path. I think your last idea , all the build under home page and a flag to filter them on a page tab (?), is the best solution, if need i can create children and path still correct. Is there any module to make it or i must create new one? 

Thanks

  

Link to comment
Share on other sites

Just want to point out that you can also do this with the new URL hooks https://processwire.com/blog/posts/pw-3.0.173/#introducing-url-path-hooks

I can't test it now, but something like this should work (not tested!):

$wire->addHook('/{building}', function($event) {
  $name = $event->sanitizer->pageName($event->arguments("building"));
  $building = $event->pages->findOne("parent=/buildings/, name=$name");
  if($building->viewable()) return $building;
});

 

Edited by diogo
corrected the code + added sanitizer
  • Like 4
Link to comment
Share on other sites

On 9/9/2021 at 10:07 AM, diogo said:

Just want to point out that you can also do this with the new URL hooks https://processwire.com/blog/posts/pw-3.0.173/#introducing-url-path-hooks

I can't test it now, but something like this should work (not tested!):

$wire->addHook('/{building}', function($event) {
  $name = $event->sanitizer->pageName($event->arguments("building"));
  $building = $event->pages->findOne("parent=/buildings/, name=$name");
  if($building->viewable()) return $building;
});

 

Interesting solutions, i try url match works but it redirect to complete path instead of return content Interesting solution. I've tried it, but instead of showing the content of the page in the url I'm at, it redirect to the full url ?

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