Jump to content

How to keep pages organized when managing lots of landing pages


evanmcd
 Share

Recommended Posts

Hi all,

So, I've got a site that's being migrated from Joomla (ouch) that has a bunch of pages at the root - i.e. http://site.com/page1 http://site.com/page2, etc

I know I could just create these in the admin below the home page, but, as I've got dozens and more being added all the time, I'd rather keep them as sub pages under a generic structure page, which I'd call something like "Landing Pages".  But, of course that would leave me with http://site.com/landing-pages/page1, etc

I know I could add htaccess rules for each specific page, but non-devs will adding these pages, so that's not a good option.

I've looked at Netcarver's URL Shortener module a bit, and that may be an option.  But, ideally I'd be able to have it managed all from the page itself.

What do you all think is the best way to manage this?  

Thanks.

P.S. Working with Joomla has made me appreciate PW all the more.  I'm sure it's good for some people but... yuch, not for me :)

  • Like 1
Link to comment
Share on other sites

So, you mean like this...

1) get urlSegment1

2) check to see if there's an existing Page with that name as a child of landing-pages

3) if so, send the user there

4) if not, do nothing and let PW handle it

I like that idea, and am gonna try it out.  Will let you know...

Thanks.

Link to comment
Share on other sites

So it seems that the urlSegment method will work.  Here's the code:

if($input->urlSegment1) {
	$has_redirect = $pages->get("/landing-pages/")->find("name|vanity_url=".$input->urlSegment1)->getTotal();
	if($has_redirect > 0) {
		$redirect_page = $pages->get("name|generic_text=".$input->urlSegment1);
		header("location:" . $redirect_page->url);
	} else {
		header("location: " . $pages->get($config->http404PageID)->url);
	}
}

It works except for one small oddity: the or check ( | ) in the selector, to check to see if the segment is either in the name or the vanity_url fields doesn't work. if the urlSegment is only in the vanity_url field, the getTotal() call returns 0.  When urlSegment is the name of the page it works fine.

Anyone know why that might be?

Link to comment
Share on other sites

Oh, whoops I left generic_text in there cause that's the actual field name I'm using.  I just used vanity_url in the example so it would be a bit more clear that the field is being used to check for an alternate (shorter) way to access the page.  So the name of a page might be 10-tips-for-a-better-lawn-this-spring while the vanity_url might be just lawn.  I want either /lawn or /10-tips-for-a-better-lawn-this-spring to redirect to /landing-pages/10-tips-for-a-better-lawn-this-spring.

Hope that clarifies it.

Link to comment
Share on other sites

Ok sorted it out. You can't use multiple or | with name field in a selector, since name it's not a text field like others. 

So to make it easy you would have to first check if there's one with "name" found or check for further fields.

Here's a working code with some notes you would have in your home.php at the top. 

if(count($input->urlSegments) > 1) {
    // not more than two segments
    throw new Wire404Exception();
}

if(count($input->urlSegments) == 1) {
    // sanitize to make sure it's valid page name
    $seg1 = $sanitizer->pageName($input->urlSegment1);

    // search for name since name can't be used with or "|" multiple field selector
    $found = $pages->get("parent=/landing-pages/, name=$seg1");
    if($found->id) $session->redirect($found->url);

    // if page with name of url segment not found search alternative fields..
    $found = $pages->find("parent=/landing-pages/, short_url=$seg1")->first();
    if($found->id) $session->redirect($found->url);

    // if we reached here, nothing found, throw a 404
    throw new Wire404Exception();
}

In case you want to leave at the short url and not redirect, you could just render the found page and exit:

if($found->id){
    echo $found->render();
    exit(0);
}

In case you got questions just ask.

  • Like 4
Link to comment
Share on other sites

Wow, thanks Soma for the detailed response.  You're code is great - a few nice PW tricks in there I hadn't realized I could use.  Also glad to know I wasn't going crazy thinking that or selector should work as written, and to know the gotcha re the name field.

Much appreciated, this is just the solution I was looking/hoping for.

Link to comment
Share on other sites

  • 2 years later...

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...