Jump to content

URL Shortener


netcarver
 Share

Recommended Posts

I had to setup a similar thing for the cmscritic.com site. Previously it had been running WordPress, which had all the articles living off the root URL. We wanted the new site to maintain all the same URLs as the WordPress site, rather than setting up 301s. So all the articles still live off the root URL, despite the fact that they are actually located within the tree. This is the case for other pages in the site as well. As an example of how to do it, see the cmscritic case study, and the /site/templates/home.php template file code in particular. 

  • Like 1
Link to comment
Share on other sites

I suppose you want to do this for SEO reasons, but like Diogo said, changing parents like that is not good practice...

Not for SEO. I am not sure in  what  section (parent page)  an article (child page)  will be. Today it is under  site.com/poetry/milton, tomorrow it can be here site.com/poetry/american/revival/milton

I had to setup a similar thing for the cmscritic.com site. Previously it had been running WordPress, which had all the articles living off the root URL. We wanted the new site to maintain all the same URLs as the WordPress site, rather than setting up 301s. So all the articles still live off the root URL, despite the fact that they are actually located within the tree. This is the case for other pages in the site as well. As an example of how to do it, see the cmscritic case study, and the /site/templates/home.php template file code in particular. 

thanx. yes, that's what may be needed. I will take time to read it. it is not that easy to implement for a beginner.

Link to comment
Share on other sites

Hi xeo and welcome to the forum.

Today it is under  site.com/poetry/milton, tomorrow it can be here site.com/poetry/american/revival/milton

I'm just curious as to why you might be changing the location of children around? Would that be happening regularly? Could you not just put it directly under its ultimate parent?

Link to comment
Share on other sites

Hi xeo and welcome to the forum.

I'm just curious as to why you might be changing the location of children around? Would that be happening regularly? Could you not just put it directly under its ultimate parent?

Well, from my experince I know that  sometimes as site grows  there may be more sections / categories  that  article can be be placed in the future that one cannot forsee right now (like in my example).  By the way, TXP is good at it, i am sure you know this.     

Link to comment
Share on other sites

Xeo - generally most of us would recommend that in a situation where categories can change that you might instead put an article for Milton under articles/milton and create your list of categories separately and tag Milton against different categories.

This way it doesn't matter if the categories change or become more complex later on, the article is still under articles/milton and the categories can change as much as you like (in your example Milton could be tagged in the poetry category to begin with, but also under all of the child categories (american and revival in your example) later on so that you have lots of articles listed in the parent category of poetry and the list becomes more refined the further down the categories you go.

Basically that way you're not breaking any URLs and can tag things easily in multiple categories - for example if you moved Milton to poetry/american/revival/milton but then had a category called poetry/poets and wanted milton under there too you would have a problem, so best to put the article itself under something like articles/milton instead and associate the articles with the categories separately so you can have that  multi-category relationship going on.

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Hey guys,

I'm building a site that has some rather long urls going on which is going to be a problem because there are going to be some social media buttons on the website, specifically a twitter share button.  This module is looking to be a good solution but I had been hoping to generate the short urls rather than depending on the client to create them.  

I've created a module that hooks into page save and creates a new short url if an existing on for that page is not found.  My issue is that it creates too many short urls.  I've tried hooking into different aspects of the page save to see if it makes a difference (which it does) so I'm suspecting that I may not fully understand hooking (as I haven't used it too many times). 

$this->pages->addHookAfter('save', $this, 'createLink');     creates 2278 links

$this->pages->addHookAfter('added', $this, 'createLink');  creates 2248 links

$this->pages->addHookAfter('saved', $this, 'createLink');   creates 1124 links

Except for the first link, which is correct, each link refers to the previous link. 

Here's what I have:

/**
 * Initialize hooks
 */
public function init()
{
	$this->pages->addHookAfter('save', $this, 'createLink'); 
}

/**
 * createLink hooks into the pages->save method and creates a new short url if an existing one is not found
 *
 */
public function createLink($event) {
	$page = $event->arguments[0]; 
	$full = $page->url;
	$l = wire()->pages->get("template=LinkShortener,full_link=$full");
	if($l instanceof NullPage)
		LinkShortener::addNewLink( $full, $page->id );
}

Any help you can offer would be appreciated. 

Thanks!

Riz

Edit: 

I've discovered my super rookie mistake. I didn't take into consideration that when the short url was created it was triggering a page save which was what was creating an infinite loop which ended up timing out when it got somewhere in the thousands.

Changed it to:

public function createLink($event) {
	$page = $event->arguments[0]; 
	$full = $page->url;
	if ($page->template != "LinkShortener"){
		$l = wire()->pages->get("template=LinkShortener,full_link=$full");
		if($l instanceof NullPage)
			LinkShortener::addNewLink( $full, null );
	}
}

And it works great.

Edited by rizsti
  • Like 2
Link to comment
Share on other sites

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