WordPress Simple Redirect PHP Modification

After I had completed the move of the air traffic related entries off this blog to ATC Freqs, I realized that I didn’t have a plan to address links to the posts that had been moved.

As a result, trying to view posts at the original location that had been moved would return a message of, “Sorry, no posts matched your criteria.”

Since I wasn’t happy with that I wanted to come up with a solution to redirect to posts on the new site.

The way I performed the move of the blog entries was to do a simple WordPress export of all the entries on this blog, then an import into the new WordPress installation at the new site.  Then I simply deleted the non-pertinent entries from the new site, and then did the same here.

The reason I got lucky is that by doing an export/import, I inadvertently preserved the WordPress post entry IDs for the imported entries on the new site.

In other words, when I imported the entries, an entry with a link such as “http://www.ruralgeek.com/wpblog/?p=2645&cpage=1″ ended up on the new site at “http://www.atcfreqs.com/wpblog/?p=2645&cpage=1″.

Thus, in order to forward links from the old site it would be simple task of redirecting to the new site, using the same post ID and just changing the domain (and directory if necessary).

Initially I tried some redirect plugins for WordPress, but in the short period of time I messed around with them couldn’t figure out how to do what I wanted to do.  I’m sure one of them does what I wanted, but I opted to figure out a different solution.

I decided to modify the WordPress single.php (Single Post) file instead and made some minor changes to eliminate the short “Sorry, no posts matched your criteria.” message with one containing the link to the post on the new site.

(I’ll disclaim here and mention that I’m no PHP guru by any means; there may be a more elegant way of doing the following!)

Since I couldn’t find a WordPress function to return the original URL of the errant link I needed another way to determine the URL of the post.

The PHP code snippet to return the URL of the current page was found here and I added it to the single.php file first:

<?php
function curPageURL() {
$pageURL = ‘http’;
if ($_SERVER["HTTPS"] == “on”) {$pageURL .= “s”;}
$pageURL .= “://”;
if ($_SERVER["SERVER_PORT"] != “80″) {
$pageURL .= $_SERVER["SERVER_NAME"].”:”.$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>

Then I modified the error message (“Sorry, no posts matched your criteria.”) replacing it with the following code:

<p>Sorry, that post wasn’t found here.</p>
<p>Some of the posts that were previously here have recently been moved to the <a href=”http://www.atcfreqs.com”>ATC Freqs blog.</a></p>

<p>This may be the page you were looking for: <a href=<?php echo str_replace(“www.ruralgeek.com”, “www.atcfreqs.com”, curPageURL());?>> <?php echo str_replace(“www.ruralgeek.com”, “www.atcfreqs.com”, curPageURL());?>
</a></p>

The result is a page such as this, that gives a more clear error message as well as a link to the location of the new page.

Obviously you would need to modify the “str_replace” statements with the appropriate strings for your sites, including the directories if they changed, as well as the message text referring to the new site domain.