How to get the URL for a WordPress site

How do you fetch the URL for a WordPress site?

The problem

My wife has started a new business focusing on the Swedish healthcare market. With the main company website up and running, she then aquired two more websites with fairly different domain names. Neither of which can be found through a simple search for select keywords on Google.

These websites featured the same, identical content. The reason for the identical content where that the domain names where essentially synonyms (equivalent words) for a specific profession. The idea where to drive traffic to either site and have them serve useful information for the profession in question (such as average salary for the last three years etc). Now, Google doesn’t look kindly on duplicate content (as some of you web savvy people already know). Duplicate content can make a website suffer greatly when it comes to search results; Heck the site might not turn up at all! That is very bad news for a small company trying to make its presence known on the world wide web.

The solution for this problem was to either do a 301 redirect or make use of a Canonical url’s. There is a lot of information to be read on the internet about when to use 301 redirect and when not, or when to use Canonical url’s instead. In essence a 301 redirect tells the search engines that a page has permanently moved to a new address; Thus the search engine will remove the old address/page from the index and from thereon pass visitors and credit to the new page. A canonical url simply states that there is multiple versions of this page (~ duplicate content). A canonical url then goes on to specifically point out the master version. The master version is the only version that will be indexed by and get any credit from search engines. All other versions is still available through favorites/bookmarks or surfing to them directly through making use of the address bar in the web browser; However, they are not indexed.

As my wife wanted both websites available, as she have marketed them both, there was but one choice really. I had to make use of Canonical url’s. Now, fortunately, one of the websites recieved considerable more traffic than the other. Making rounds with different SEO tools confirmed that the keyword for that domain was more popular. I needed then not to worry about deciding which site should point to the other. What I needed to do was simply to point to the more popular domain from the lesser so. But as there is quite a few pages and a search function to consider – it wasn’t all quick and easy. All the pages had to be pointed to the equivalent pages on the more popular site etc. Hence, it is not enough just to point to the base URL (ie. www.example.com). The URL in the canonical link needs to be absolute, not relative. Thus I needed to get all parts of the URL including the search query string (if available). Heres how a canonical link should look like:

<link rel="canonical" href="http://example.com/original-url.html">

The solution

What follows is a simple PHP example snippet that could be used to write out the canonical url (it needs to be placed after the HEAD in the html code). Use it cautiously as canonical links can backfire if wrongly used!

<?php
// URL VARIABLES

// Base URL, ie. www.mathiaskholm.se
$url_base = $_SERVER['SERVER_NAME'];

// Last part after the base URL without query string 
// variables
$url_part = add_query_arg(array(),$wp->request); 

// Last part after the base URL with query string 
//variables
$url_partandquery = $_SERVER['REQUEST_URI'];

// Full URL including query string variables
$url_full = $url_base."".$url_partandquery;

// WordPress search function command
$url_searchterm = get_search_query();

// Print out the first part of the canonical url

  echo '<link rel="canonical" href=';

// What page are the user viewing? 

if ($url_part == "") {
  // The $url_part is empty meaning that the user is 
  // viewing the frontpage or is searching for 
  // something. Lets find out what.

  if (!strpos($url_full, '?')) {

    // No query string thus the user is viewing the 
    // frontpage. Lets print out the canonical url.

    echo '"http://'.$url_base.'">';

  } else {
 
    // The user is searching for something. Lets found 
    // out what. Then print out the canonical url.

    echo '"http://'.$url_base.'/s='.$url_searchterm.'">';
  }

} else {

  // The $url_part contains something - Thus the user is 
  // viewing an individual news post 

  echo '"http://'.$url_base.'/'.$url_part.'/">';
}
?>

Leave a Reply

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