The Right Way To Restructure Your Site URL Structure Without Losing Ranks And Traffic

Url-restructuring-without-loosing-traffic

There are times when you just want to change your permalink structure but don’t want to risk breaking internal links. May be you want to deploy SILO URL structure to your site but the risks outweigh the benefits.

Well I attempted the same on my site(s) as and when required and I have also done it for several of my clients who wanted to deploy a SILO information architecture on their site. While SEO impact and effect varies, this technique does allow internal link restructuring with minimal impact.

That said, it’s not all that easy. When you restructure your site, you do risk breaking the incoming links which still remains the main ingredient of Google’s ranking. (Not much innovation by Google on that front; they are still being fed by their only product even after all these years — AdWords).

So here’s a two-pronged strategy:

  1. Fix internal links
  2. Redirect incoming links to their correct URL

1. Fixing internal links

The word permalink is misleading at best. It’s sounds as if it refers to permanent links but it is not permanent at all. The permanent link to a post is the shortlink in the true sense.

So how do we go about changing all internal permalinks to shortlinks?

Well, we look for any links in the post/page content. Once we find a match, we replace it with the shortlink and update the post content.

While you can do a complicated routine to replace things in the database, I actually designed the code such that it works on archive pages. I set the blog page to show 50 posts per page and just clicked through the blog pages till the last page. For the pages you’ll have to go one by one.

function bt_replace() {
	$post_id = get_the_id();
	if ( 'page' == get_post_type($post_id) || 'post' == get_post_type($post_id)) { // If we are on a post or a page (we want to avoid attachments etc.)
		$content = get_the_content(); // get the unfiltered content
		$siteurl = get_site_url(); // get the address of the current site
		$siteurl = trailingslashit($siteurl);
		$siteurl = preg_quote($siteurl); // 
		$exp = "/href=["|']($siteurl[^"']+)/";
		
		$content = preg_replace_callback($exp, 'bt_replace_url', $content); // replacement magic. Match urls begining with our WP site.	

		if(is_user_logged_in()) { 
			wp_update_post( array('ID' => $post_id, 'post_content' => $content) ,$error); // update the post with the new content
			bt_log($error);
		}
	}
}

function bt_replace_url($matches) {
	$postid = url_to_postid( $matches[1] ); // get the post id from the matched url
	if ( $postid && ('page' == get_post_type($postid) || 'post' == get_post_type($postid))){ // If this is the link to a post or page (we want to avoid attachments etc.)
		$shorturl = wp_get_shortlink($postid); // convert it into shorturl
		return str_replace($matches[1], $shorturl, $matches[0]);
	}
	return $matches[0];
}

add_action('genesis_before_entry','bt_replace'); // any hook within the loop should do

function bt_log($str) {
	echo '<pre>';
	print_r($str);
	echo '</pre>';
}

Once you’ve replaced the permalinks with shortlinks you can change the permalink to virtually anything and internal links won’t break.

2. Fixing incoming links

Well you can’t go around external blogs, social sites and change every link that you’ve shared in the last several years. But a 301 redirect should take care of that.

One big mistake I’ve seen people do is that they redirect 404 links to the homepage. We’ll play smart. We’ll detect 404s in advance and find the matching post and then do a 301 redirect. This means that the existing ranks are passed to the same content on the new url.

For this purpose I recommend the 404 Redirected plugin which creates automatic redirects for 404 traffic and page suggestions when matches are not found providing better service to your web visitors.

The above 2 steps are critical to keep the user-experience and rankings consistent. If you want to deploy a SILO information architecture on your WordPress site, feel free to drop me a line.

Here’s a success story of successfully recovering traffic after URL restructuring.

Comment on The Right Way To Restructure Your Site URL Structure Without Losing Ranks And Traffic

Your email address will not be published. Required fields are marked *