Jump to content


Photo

How do I create a page that redirects to it's first child?


  • Please log in to reply
4 replies to this topic

#1 ryan

ryan

    Hero Member

  • Administrators
  • 5,780 posts
  • 3125

  • LocationAtlanta, GA

Posted 29 December 2010 - 01:05 PM

I really need to create pagetype, that will redirect me to first of it's children... I.E.

- Page 1 {/page1/}
- Child 1 {/page1/child1/}
- Child 2 {/page1/child2/}

and when you click on 'Page 1' in menu, you'll go straight to '/page1/child1/'.


For pages that you want to automatically direct to the first child, I would make them use a template that has this in it's code:

<?php if($page->numChildren) $session->redirect($page->child()->url);

And then, when you add another child, the internal pointer will redirect there.


If you add a new child page, and they aren't sorted by date added (descending) then the newly added page isn't likely to be the first child. If no default sort is selected, then it'll add it as the last child. So if you wanted to redirect to last page (rather than the first) then you'd want to do this:

<?php if($page->numChildren) $session->redirect($page->child("sort=-sort")->url);

By the way "sort" is just a name representing the order that pages have been dragged/dropped to. That's the default value if a page doesn't have another field selected as it's default sort field. The minus sign "-" in front of it represents descending sort. Without the minus sign, it would be ascending. Since you want to select the last page, that's why it's "-sort" rather than "sort".

Or if you wanted to make it select the most recent page added by date:

<?php if($page->numChildren) $session->redirect($page->child("sort=-created")->url);

The above is also descending since most recent added would be the highest date.


#2 totoff

totoff

    Distinguished Member

  • Members
  • PipPipPip
  • 87 posts
  • 39

  • LocationCologne, Germany

Posted 01 March 2013 - 06:46 AM

Hi Ryan,

 

I wonder if this technique may have any negative effect on search engines as it doesn't return a 301 that can be understood by the bots? What is your opinion/experience?

 

Best, Christoph



#3 Wanze

Wanze

    Sr. Member

  • Members
  • PipPipPipPip
  • 362 posts
  • 381

  • LocationBern, Switzerland

Posted 01 March 2013 - 06:53 AM

Hi totoff,

It does set header 301 if you use Session::redirect by default.

Here's the method from the Session class:

 

<?php
	public function ___redirect($url, $http301 = true) {

		// if there are notices, then queue them so that they aren't lost
		$notices = $this->fuel('notices'); 
		if(count($notices)) foreach($notices as $notice) {
			$this->queueNotice($notice->text, $notice instanceof NoticeError ? 'error' : 'message', $notice->flags); 
		}

		// perform the redirect
		if($http301) header("HTTP/1.1 301 Moved Permanently");
		header("Location: $url");
		header("Connection: close"); 
		exit(0);
	}


#4 totoff

totoff

    Distinguished Member

  • Members
  • PipPipPip
  • 87 posts
  • 39

  • LocationCologne, Germany

Posted 02 March 2013 - 04:44 AM

hi wanze,

 

thanks pretty much for your reply. as i'm a beginner with php and in particular with object orientated programming could you give me a hint how to implement this? do i have to "hack" the core (i.e. amend the session class)?

 

best, christoph

 

EDIT: read twice through your reply and think i got it on the second try ;-) pw is setting 301 header by default, right? no need to edit anything.



#5 SiNNuT

SiNNuT

    Sr. Member

  • Members
  • PipPipPipPip
  • 367 posts
  • 232

Posted 02 March 2013 - 06:23 AM

you got it right christoph






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users