Jump to content

Virtual or Redirect or Fake Pages


suntrop
 Share

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 :-)

 

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

Link to comment
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;

 

Link to comment
Share on other sites

  • 9 months later...
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.

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

×
×
  • Create New...