Jump to content
suntrop

Virtual or Redirect or Fake Pages

Recommended Posts

Sometimes I need pages in the site tree that are basically just a reference to another page – to redirect to the original page or display the same content under the new page url.

I was surprised I couldn't find anything like that in the forum or as a module already. However, there is a new module by @Robin S but not exactly what I need.

Example

  • Home
  • News
    • Press
    • Newsroom
    • Media
  • Services
  • About
    • Newsroom (virtual page for /news/newsroom/)
  • Contact

Example I: The page /about/newsroom/ should act as /news/newsroom/ – so, wehen I loop all pages for a sitemap both pages should have the URL /news/newsroom/.

Example II: Another example (not often used) would be tu display the exact same content on /about/newsroom/ – although the page itself has no content on its own.

Those examples don't have much in common, but they are just virtual or fake pages in the site tree.

While the second one should be an easy task with wireRenderFile(), the first example isn't quite obvious to me. I think I have to use hooks and I have no idea where to start. I really appreciate you guys come up with a solution for everything, but I need to learn this myself :D So just some hints to start would be great!

  1. Do I put the code into a module or some of the _init.php, ready.php etc. files? Or do I have to use a template file (e.g. virtual-page.php)?
  2. When I look into Captain Hook  – how do I find the appropriate point to hook into? (/wire/core/ Page.php and loaded()?)
  3. Do I need to create/manipulate each field separately (path, title, summary)?

Sorry for the long read :-)

 

Share this post


Link to post
Share on other sites

I'm sure other people have better solutions, but I usually handle this with redirects.  I usually create two templates:

  • redirect-internal
  • redirect-external

So in your example, I would create a page for about/newsroom/ that uses an redirect-internal template and redirects to news/newsroom/.

Here is what my fields look like for redirect external:

59f1faa537dbc_Templateredirect-external.thumb.jpg.cc62021fc88da36308b1b407868898d5.jpg

Here is what my fields look like for redirect-internal:

59f1fada8ca77_Templateredirect-internal.thumb.jpg.7fd2efe4290b03d42adde886a3c1cb47.jpg

redirect-external.php

<?php namespace ProcessWire;

$session->redirect( $page->redirectTo );

redirect-internal.php

<?php namespace ProcessWire;

$session->redirect( $page->redirectToPage->url );

 

Edited by gmclelland
  • Like 3

Share this post


Link to post
Share on other sites

Hi gmclelland. Thanks. Yes, that would be a way I would go. Maybe it's just me, but I would prefer not having an "invalid" URL – although if implemented as 301 it wouldn't be indexed by Google (but nonetheless doesn't count as a good link).

And as I said, I really need to learn more about hooking into PW :D 

Share this post


Link to post
Share on other sites
5 hours ago, suntrop said:

display the same content under the new page url

This is known as duplicate content and you want to be careful with this to avoid harming your SEO. Have a read of Google's guidelines:
https://support.google.com/webmasters/answer/66359?hl=en
https://support.google.com/webmasters/answer/139066

But if you want to show all the content from one page on another, one simple solution is to add a Page Reference field to your template. Then at the top of the template file:

if($page->page_reference->id) $page = $page->page_reference;

 

Share this post


Link to post
Share on other sites

Thanks for pointing to DC, sometimes it is ok to have it :) And that is some quiet simple code snippet ! Thanks again.

Share this post


Link to post
Share on other sites

Thanks to gmclelland. His code works really good.

Thanks also to Robin S for pointing out the problems with duplicating the pages. His code also works, but I decided for the redirect solution in order to not confuse bots.

Share this post


Link to post
Share on other sites
On 10/26/2017 at 10:41 PM, Robin S said:

This is known as duplicate content and you want to be careful with this to avoid harming your SEO. Have a read of Google's guidelines:
https://support.google.com/webmasters/answer/66359?hl=en
https://support.google.com/webmasters/answer/139066

But if you want to show all the content from one page on another, one simple solution is to add a Page Reference field to your template. Then at the top of the template file:


if($page->page_reference->id) $page = $page->page_reference;

 

It's not as easy. This can lead to all sorts of problems as you only change the template var $page and it may work for simple cases. But PW already has set and loaded the requested page internally at this point and other code (in templates, modules etc) may not work correct as they will still ”see" the other page.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By stanoliver
      Hi! 
      The following code snippet is part of my markup simple navigation and the url_redirect (url field in the backend) just works fine when I put an special custom url into the url_redirect field.
      <?php $nav = $modules->get("MarkupSimpleNavigation"); // topnav $current = $page->rootParent(); // current root page // subnav echo $nav->render(array( 'max_levels' => 2, 'item_tpl' => '<h4><a class="g8-bar-item g8-hover-black" href="{url_redirect|url}">{title}</a></h4><hr class="sidenav">', 'collapsed' => true, ), $page, $current); ?>  In my seperated breadcrumb navigation I use the following code snippet
      <?php foreach($page->parents()->append($page) as $parent) { echo "<li><a href='{$parent->url_redirect|url}'>{$parent->title}</a></li>"; } ?> Now to the problem: In my first code snippet above the
      url_redirect|url 
      works just fine but when I try something similiar in the second code snippet
      $parent->url_redirect|url
      I produce an server error How do I have to change the second code snippet so that it works in the correct way as the first code snippet does?
    • By VeiJari
      Hello forum!
      I've yet again stumbled on a head-scratching situation. We have enabled the option on our articles template and events template that it skips the title adding part and goes straight to the form. This is what our customer wants. So when you add a new article or event it automatically names it temporary to "article-0000000" and same with event. Now the problem is that obviously after saving the form we want to change to page url or "name" to the title, like it's normally. 
      Now here's the code for the hook:
      wire()->addHookBefore("Pages::saved(template=tapahtuma|artikkeli)", function($hook) {
        $page = $hook->arguments(0);
        $newUrl = wire()->sanitizer->pageName($page->title); // give it a name used in the url for the page
        wire()->log->message($page->name);
        $page->setAndSave('name', $newUrl);
      });
      I get the correct page and the name and path changes when I log them, but when I try to save it. It just loads and then I get: 
      Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) This happens in sanitizer.php
      and then another error: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0
       
      What is happening? Am I not suppose to use sanitizer in this way? When we made a temporary page object in out other hook, the sanitizer worked perfectly.
      Thanks for the help!
    • By VeiJari
      Hello forum! I'm trying to learn how to time up my functions by using lazycron but I can't get it fired up. 
      Here's my code in site/init.php:
      function setEventToRepeat(HookEvent $e) { $wire->log->message("kutsutaan cron"); $events = $wire->pages->find('template=tapahtuma'); die(var_dump($events)); $wire->log->message('Kaikki tapahtumat haettu:' .$events); }   $wire->addHook('LazyCron::every30Seconds', null, 'setEventToRepeat'); I get nothing in the logs and can't echo or dump anything
      Thanks for the help!
    • By Orkun
      Hi Guys
      How can I make a redirect inside the .htaccess to my custom maintenance.html file when any URL of my Website is accessed except the processwire admin (www.example.com/processwire/).
      Because I want that my User's still can access the website when they are loggedin in Processwire.
      When the current url starts with /processwire or if there is a processwire-login-cookie (Is there a cookie when user is logged in Processwire?) available the redirect should not work. Otherwise it should work.
      How can I achieve this?
    • By louisstephens
      So I have been diving into hooks lately, and I am enjoying them thus far. However, I guess I am a bit stumped on how to achieve what I want too. I am trying to set up a hook that would create a new child page when the parent page is saved. However, when you save the parent page a second time, I just need to update the child page without creating multiple child pages. What would be the best way to go about this?
      So after rereading my post, I believe it is a bit vague so I will try to explain more. 
      The Goal:
      Create a page with a template "one". Once the page is created/saved => create a new child page with the template of "two" If the parent is saved anytime after, do nothing to the child page (limit the parent page to one child page) The parent page is really just being used to output content, whereas the child page is being used to pull out the some fields from the parent to be used elsewhere. I might have made this too complicated in my head.
×
×
  • Create New...