Jump to content
ajt

Ignore a parent in URL (but allow grandchildren)

Recommended Posts

Hello!

I want to keep sort some pages in my page tree within a single parent, but then have the URLs for those pages ignore their topmost parent.

So for example, if the page is at: [home]/buildings/architect-name/building-name
I want the URL to be /architect-name/building-name

I've search around the forum, and the solution that comes up everywhere is a version of the code from this post

/**
 * This hook modifies the default behavior of the Page::path function (and thereby Page::url)
 *
 * The primary purpose is to redefine blog posts to be accessed at a URL off the root level
 * rather than under /posts/ (where they actually live). 
 *
 */
wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'post') {
    // ensure that pages with template 'post' live off the root rather than '/posts/'
    $event->replace = true;
    $event->return = "/$page->name/";
  }
});

And I can use a version of that to successfully get buildings/architect-name to appear as /architect-name
But /buildings/architect-name/building-name still appears as /buildings/architect-name/building-name

Is there a way to get URLs to ignore/remove that topmost parent even when the page has a grandchild?

Any help much appreciated!

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Sébastien
      Hello everyone,
      I recently started using ProcessWire and already manage to accomplish several things on my own, but I keep asking myself a lot of questions, among which the following:
      After installing a multiple sites version (first method), everything works for now as desired, but I realize that all the files seem accessible by all the domains.
      Let's say that site1.com can access the asset files, template or other ressources of site2.com folders and vice versa by specifying only and for example:
      site1.com/path_to_a_site2_folder/one_file
      or
      site2.com/path_to_a_site1_folder/one_file
      Would you know if there is a simple way to prohibit this kind of behavior in order to clearly distinguish each site and thus ensure the confidentiality and separation of content?
      I prefer to avoid the entities to be considered as a single set by crawlers and prevent access the elements belonging to each domain from another domain.
      Thanks for your help. Sébastien.
    • By Robin S
      Add Image URLs
      Allows images/files to be added to Image/File fields by pasting URLs.

      Usage
      Install the Add Image URLs module.
      A "Paste URLs" button will be added to all image and file fields. Use the button to show a textarea where URLs may be pasted, one per line. Images/files are added when the page is saved.
       
      https://github.com/Toutouwai/AddImageUrls
      https://modules.processwire.com/modules/add-image-urls/
    • By Guy Incognito
      Have read loads of similar threads but can't find the deifnitive answer. I'm bootstrapping ProcessWire into a Magento installation. Everything is working fine in terms of expected PW API functionality, however I cannot get the paths for images in the site work correctly.
      ProcessWire is installed in a subfolder called 'pw'.
      If I load the PW site directly e.g. visit mainsite.com/pw everything loads fine and all image paths are correct. If I load the parent site in the root folder with the PW bootstrapped page content rendered within it, the image paths don't contain the subfolder and so are broken. So I get
      https://mainsiteexample.com/site/templates/img/test.jpg instead of
      https://mainsiteexample.com/pw/site/templates/img/test.jpg I've tried updating the rewrite base in the .htaccess file but it doesn't seem to make any difference. Have also tried various settings in the config file to no avail. Wondering if rewrite base perhaps isn't working as intended because I'm behind a NGINX/Apache hybrid environment?
    • By louisstephens
      So I have a project where multiple pages are sending POST data to 1 single template page.  All was working well (well, at least with one ajax post), but now I have hit a stumbling block. I figured  the "best" way to handle the request were to use url segments and then use the following in the status page:
      if ($config->ajax && $input->urlSegment1 == 'add-bookmark') { // some code here } However, this doesnt seem to really work (as I assume the the request isnt being posted to /status/ but rather to /status/add-bookmark/). What is the best way to actually handle this?
    • By Noel Boss
      Maybe I'm missing something here, but I'm haveing a hard time to get absolute urls if PW is installed in a subdirectory thought the API...
      I need an absolute URL for the backend (as a redirect url of Oauth provider). To build the url i would normaly use
      url()->root.url()->admin – but this results in the install-path beeing present twice;
      domain.com/path/ + path/admin/
      In order to not use $_SERVER['HTTP_HOST'] I fetch the admin page from pages – but this is also not to easy since I need to remove the root folder from path as well. My current solution is somewhat complicated:
      $backendUrl = pages()->get('path="'.str_replace(urls()->root, '', urls()->admin).'", include=all')->httpUrl; The above code seems to be the savest way to get the absolute url to the backend using pure PW API, am I right?
      It would be great if one could fetch absolute urls with something like this
      $backendUrl = urls()->admin->httpUrl // or $backendUrl = httpUrls()->admin  
      SOLVED:
      Thank you @adrian – of course this HAD to be simpler:
      urls()->httpAdmin;  
×
×
  • Create New...