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 ridgedale
      Reference: PW 3.0.111 and uikit3 based site using the Regular-Master profile.
      I'm trying to automatically redirect a logged-in user to a custom profile page using $session->redirect() and need to add $user->name to the redirect path.
      All my attempts appear to have failed:
      $session->redirect('/user-profile/')->name; $session->redirect('/user-profile/')->$user->name; $session->redirect('/user-profile/' . get($user->name . '/')); $session->redirect('/user-profile/' & get($user->name)); Can anyone point out where I am going wrong?
    • By quickjeff
      Hi Guys,
      I am running a page template that contains a filter form that allows users to display results based on number of  comments. 
      Here is what I am thinking about doing: 
      -add new field called total_comments 
      -somehow populate this field every time a new comment is added or removed by admin with count($page->comments)
      -use form filter to display results that the selector form has found.
      Example: $selector = “total_comments>=$q”; 
       
      My question to everyone: Is this a good approach? If so how do I get another field to update based on the comment module adding or removing a comment? 
    • By Mustafa-Online
      I got a field with a type of "files" named: course_file .. how can I upload it to custom directory??
    • By dragan
      $this->addHookBefore('InputfieldTextarea::render', function($event) { $field = $event->object; if($field->name == 'body_offer') { $del = ''; foreach($this->wire->pages->get(11229)->textblocks as $item) { $field->entityEncodeText = false; $body = str_replace(PHP_EOL, '', $item->body); $body = addslashes($body); $title = $item->title; $field->description .= $del . "<a href=\"#\" class=\"ckesnippet\" data-snippet=\"$body\">$title</a>"; $del = ' | '; } } $js = wire('config')->urls->templates . 'scripts/ckesnippets.js'; $event->return = str_replace("</html>", "\n<script type='text/javascript' src='$js'></script>\n</html>", $event->return); }); I'm trying to add custom text-blocks from repeater fields into CKEditor. I've stolen the whole idea (and code) from @bernhard

      Problem is: The links are actually being rendered, but the Javascript is not loaded, i.e. the $event->return line doesn't seem to work. If I inspect the HTML, my additional script-tag is nowhere to be seen. I don't get any errors either. Running latest PW dev + PHP 7.1.19.
    • By Marco Angeli
      Hi there,
      I added a ssl certificate to my site and I'd like to redirect every single http url to its new https version
      So I added this code in the .htacces file, after the RewriteEngine On :
      Redirect 301 /about https://www.mysite.it/about
      Unfortunately this is now working: I get the "too many redirects" error.
      The following code works, but it's a bulk redirection to the home page, something I don't want for SEO reasons (https://moz.com/blog/save-your-website-with-redirects😞
      RewriteCond %{HTTP_HOST} mysite\.it [NC]
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mysite.it/$1 [R,L]
      Any suggestions?