Posted In Internet

Improved Permalink Redirection

After a hard evenings work, I have a much better redirection method to replace the one I described in this post. Previously, I was simply guessing which post a searcher was looking for and displayed a link to that post.

That was all fine and dandy, but I have pretty good search ranking for various keywords. I’d like to keep it that way. After digging around a bit I came across the best method to keep my search rankings in place and manage to redirect the searcher to the desired post. Enter the 301 Permanent Redirect.

I found a nice simple PHP function to do redirection on any number of levels. This function has the ability to send specific HTTP/1.1 status codes based on the type of redirection desired. Since my old permalinks will never be valid again, I chose the 301 Permanent Redirect. A note, the function listed at the URL linked above doesn’t work as-is, you need to modify it. The modified function is below, plus some extra code. All of that code is in my themes header.php file.

Another note
, I also removed the CSS stuff from that function that formats how the link to redirect to is displayed. I removed that because I added javascript to the function to do a javascript redirect if the headers have already been sent and PHP’s redirect() function can’t be used.

So, the function above should work well for anyone wanting to do some URL redirection. But back to the point of this post. Since my headers haven’t been sent at the time the function is run, PHP’s header() function is used to let the user-agent know how to respond. Now, that part is key in keeping my search rankings high, because search engines, or Google at least, pay attention to 301 Permanent Redirect codes. This topic at the Webmaster World forums describes a bit about why 301 redirects work for Google. GoogleGuy himself confirmed that the method mentioned there is indeed the best way to do a google-friendly redirect.

So, now that we know we’re using the best redirection method, we should be set. This is by no means an end all solution to maintaining search rankings after changing a sites permalink structure. And this method would only work if your previous permalink structure was setup like /archives/%postid%/. It could be modified to work for other environments however.

Let me explain the code at the top a little, specifically this piece:

The first line gets the URI requested by the searcher. After that, the explode function is used to split the URI up based on where slashes (/) occur. So, text before and after the slashes will get put into an array, $uri. Then the different pieces of the URI are put into separate variables, $dir and $postid. Now that we have at very least the post id, we can find the post the searcher was looking for.

The if statement checks to make sure the searcher is coming to the /archives/ page, which is how my permalinks used to be. For example, searching for “slackware 11” returns results for this site, but they URL’s are pointed at my old permalinks, and that page obviously doesn’t exist anymore. If the searcher isn’t coming in to an old archive page, the whole chunk of redirection code gets ignored.

Now for the final and most important piece of code (and probably simplest), fetching the new permalink.

The first line line is a wordpress function that grabs the permalink of a post based on it’s post id, which we have in the $postid variable from the previous chunk of code. Once the new permalink is obtained, we can pass it through the redirect function from above and all will be well. You can optionally add a status code to the redirect function, as I have for the 301 specific status code.

So, after all that, when a search engine user gets sent to one of my old permalinks, they will be redirected to the new permalink after clicking on the 301 redirect confirmation page. Dirty but it gets the job done, and supposedly will keep Google and other search engines from un-indexing some of my posts.

I think that’s about it, if you have any questions please feel free to contact me or leave a comment. I should also mention I tried this permalink redirection WordPress plugin, but it didn’t work right out of the box and I didn’t want to take the time to hack that plugin to do what I want. Oh, and before I forget, check out this case study I did with Zend back in 2000. I somehow came across it today while browsing the web.

Well, now what?

Work with Me

I'm available for hire and always taking new clients, big and small. Got a project or an idea you'd like to discuss? Startup plan but no developer to make it happen? Just get in touch, I'd love to see if I can help you out!

Leave some Feedback

Got a question or some updated information releavant to this post? Please, leave a comment! The comments are a great way to get help, I read them all and reply to nearly every comment. Let's talk. :) is proudly hosted by DigitalOcean

About these ads