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

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 jploch
      Hey folks,
      currently Iam working on a website for one of my clients and I need some advice on how to approach this in PW.
      The website is for a company, that offers holiday houses in two locations. 

      The client wants the homepage to show the first location. Normally I just have a home template for the first page, but here the URL should reflect that you are in Location 1. So when you visit the URL casamani.com it should redirect to casamani.com/location-1. Not sure if this makes sense at all.

      Whould it be bad for SEO and performance reasons to redirect home to the Location-1 page?
      Another approach would be to render the Location-1 template on the home template or do an include like discussed here.
      Here is how the tree looks:
      – Home
      – Location 1 (Homepage)
           – Creation
           – Adventure
           – Sustainability
      – Location 2 
           – Creation
           – Adventure
           – Sustainability

      Thanks for looking into this!
    • By rjgamer
      Hi,
      is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized?
      Thanks.
       
    • By Kiwi Chris
      I need to restrict editing access based on both the user role, and the value of a sub-field in a page reference field.
      eg $page->competition->closingDate 
      There's some discussion about restricting editing access based on role, and that works, but when I try to access the page via the method @Robin S suggests, the page returned is an admin page rather than the page being edited, so I don't have access to the edited page's fields.
       
    • By theoretic
      Hi guys and ladies! And thanks for Processwire!
      It appears i've got an interesting issue concerning the template-settings-based PW redirects dealing with access control. Any PW template has some access control options i.e. "Login redirect URL or page ID to render". If this option is used for a page having a template with this option filled, a redirect will occur if user is not logged in and/or has insufficient access rights.
      I like to hook PW events. In one of my current projects i decided to write an addHookBefore('Session::redirect', ...) which should store the page we are being redirected from. With "regular" redirects like $session->redirect('/somewhere/') this hook works like a charm. But it was strange to see that it doesn't work with the template-settings-based redirect.
      I'm too dumb to dive deep inside PW and to examine the whole PW session mechanism. But it could be rather logical if ANY redirect ( no matter template-settings-based or using $session->redirect() ) could be hooked in the same manner.
      Okay okay i can forget about template-settings-based redirect and write my own. Just a couple of lines of code, and it works. But it's less elegant than hooking the template-settings-based redirects.
      So am i missing something? It this behavior a bug, or is it intended by PW team? Thanks in advance for any comment!
    • By picarica
      so i am trying to put CustomHooksForVariations.module, a custom module, i am placing it into site/modules direcotry yet my modules page in admin panel gives me errors
      so this is the screen show when i refresh modules, i dont know why the shole hook is written on top of the page :||

      and this next image is when i try to install it, i saw that it is not defiuned modules.php but it shouldnt need to be ?, any ways i dont want to edit site's core just to make one moulde work there has to be a way

×
×
  • Create New...