Making all internal links www or non-www in WordPress

Last updated on November 28, 2011. Tags: , ,

Perhaps you've been blogging or managing websites for so long now that you already know the importance of URL canonization and consistent URL format in internal linking, like making all internal links www or non-www depending on your choice. However, that wasn't the case when you were still starting and you used both www and non-www format indiscrimately back then. Now, while you are consistent with your internal link format, you have no time to visit all the past pages and posts to revise the internal links one by one.

There are two ways wherein you can make all your internal links consistent. First is by performing search and replace on the database. Second is by inserting a little piece of code functions.php.

Search and replace on the database

In my two previous posts, I discussed methods of how to execute search and replace on WordPress' database:

Using either of these methods, search for the domain format that you do not like and replace them all with the format that you want. For example, you want all URLs to be of non-www format and you picked the plugin method, check the content checkbox, fill the "Replace" field with "http://www.yourdomain.tld" and the "with" field with "http://domain.tld" and click the "Go" button.

search and replace on URL

If you're using the the manual method, open the exported database using notepad, press CTRL + H,  fill the "Find what" form with the URL format you do not like, and the "Replace with" with the URL format that you want,  click "Replace all", and then import the database.

Inserting a code in functions.php

You can also insert a code in the functions.php to replace the URLs with the format that you like. Supposed, you want to replace all non-www internal links with the www format, open the functions.php and insert the following codes.

function canonical($text) {
$return = str_replace('http://domain.com', 'http://www.domain.com', $text);
return $return;
}
add_filter('the_content', 'canonical');
add_filter('comment_text', 'canonical');

Simply replace "domain" with your actual domain name, and "com" with your actual top-level domain (tld). The code above will change all instances of non-www internal links with the www format in both the content and the comment sections of the post.  If your preferred format is the non-www, simply switch the place of the www and non-www URLs in the code.

Note that the above code does not change the URL of the internal link in your database as seen through the WordPress post/page edit interface. It works in a way that it reads all instances of your domain as presented in one format, and then producing the other format as an actual output. Thus, site visitors and search engine crawlers will still see your preferred URL format.

Posted by Greten on December 12, 2009 under WordPress

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Post Comments





Comment Rules and Reminders

  • The links to the commentator's e-mail do not have nofollow tag. However, I will be very strict in approving comments.
  • When you comment, please say something that indicates that you indeed read my post. If your comment is a general statement that can fit to any blog post about any topic, it will be regarded as spam.
  • What you write in the name field may include keywords to your website provided that (1) it's only up to four words long and (2) at least one of these four words is your first name or nickname. I rather reply to Bob or to Joe Smith than to Online Marketing Tips.
  • Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.
  • Fields marked with asterisks (*) are required. Your email will never be displayed in public.