Forum

Thread tagged as: Question, Runway

Redirect parent to subpage

We have a parent page named Support, which has some subpages. We want to redirect this parent page to the first subpage, which is our FAQ. So when the person goes to example.com/support it redirects them to example.com/faq. Is there a simple way to do this in Perch, or will we have to do this in a .htaccess file? Thanks!

brian bower

brian bower 0 points

  • 7 years ago
Simon Clay

Simon Clay 127 points

If you go to Page Options of the parent page you can set the Path to point to the desired page, eg. /faq.php

This will send the visitor to the FAQ page when they click on 'Support' in the nav.

As a failsafe, incase someone types in the url rather than clicks on the nav, I usually add an index.php containing

<?php 
header("HTTP/1.1 301 Moved Permanently"); 
header("Location: /faq.php"); 
?>
Simon Clay

Simon Clay 127 points

In fact you could create an index template to handle this from admin:

Page template: /perch/templates/pages/index_link_template.php

<?php 
  include('../perch/runtime.php');

//setup the region and template
perch_content_create('Link to first page', array(
    'template' => 'index_link.html',
)); 

//set the redirect
  header("HTTP/1.1 301 Moved Permanently"); 
  header("Location: <?php perch_content('Link to first page'); ?>"); 
?>

And in the template /perch/templates/content/index_link.html:

<perch:content id="page" type="pagelist" label="Link to first page" />

Lastly, you'll need to install the Pagelist fieldtype https://grabaperch.com/add-ons/field-types/pagelist

Hey Simon, thanks a lot! Your last method worked great.

(Old, but gold!)

Simon, what you suggest worked fine and is really great for my client project as well… at least in theory. Your syntax for the location header gives me trouble:

  • Writing it like you did results in the redirect being (in my case) https://localhost:8888/<?php perch_content('Link to first page'); ?> because the PHP for perch_content() is not being processed, but returned to the header.

  • I searched a few PHP threads and found out that you usually should concatenate like this: header("Location: " . <?php perch_content('Link to first page'); ?>");

Both don’t work. The first results in a 404 (unprocessed PHP), the second solution prints out the right path on the page, instead of putting it into the location header.

My set up: I want to set up a number of parent pages ("website sections") with several sub-pages each. They are the first level of my main navigation.

My first try was like this: In Perch Runway, I set the parent pages’ path-option to be the path for its first sub-page. The content is correctly pulled from that sub-page, but the template used is still the one I defined for the parent. Plus, I have a parent page that has regions defined, which will not be displayed – a trap for every client.

That’s why I’ve been trying to find a way to instead redirect the parent-page to a sub-page: the parent is just a collection of other pages, there is no content for that page in itself. Perch doesn’t seem to work for that use-case (which is common for most projects I work on, which is why I think I’m overlooking something).

Got it! Storing the variable with perch_content_custom and outputting it by concatenating worked.

This is what I used to get the redirect working:

Master page "templates/pages/redirect.php" (used by the parent page):

<?php

include($_SERVER['DOCUMENT_ROOT'].'perch/runtime.php');

//set up the region and template
perch_content_create('Redirect', array(
    'template' => 'index__link.html',
    'raw' => true
));

//store redirect-path in a variable
$newHeader = perch_content_custom('Redirect', array(), true);

//set the redirect
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: " . $newHeader );

?>

Template "templates/content/index_link.html" (using the pagelist fieldtype):

<perch:content id="redirectpage" type="pagelist" label="Redirect to this page:" />

Thanks a lot, Simon! This is very comfortable, and it works even when you type the URL!