PHP Redirect Script (from PHP-Fusion to WordPress)

What problem does this script solve? If you convert your site from PHP-Fusion to WordPress (or any other system), other websites might have “deep” links (like this: (for example: http://CMSTrainingVideos.com/viewpage.php?page_id=52) into the content of your site, and you don’t want them seeing an ugly page like this:

Thus the solution follows:

<?php
Header( "HTTP/1.1 301 Moved Permanently" );
$setHeader = false;
if ($_GET["page_id"] == 1)
  {   
      Header( 'Location: http://biztalk-training.com/?p=318');
      $setHeader = true;
  }
if ($_GET["page_id"] == 2)
  {   
      Header( 'Location: http://biztalk-training.com/?p=319');
      $setHeader = true;
  }
// Now handle the case of an unknown article_id, just redirect back to home page. 
if ($setHeader == false) 
   Header( "Location: http://biztalk-training.com");
?>

The following code is saved in a file (on this site) called viewpage.php.   In PHP-Fusion, links to custom page look like this: http://cmstrainingvideos.com/viewpage.php?page_id=1.  The idea of the redirect script is to redirect, where possible, the old PHP-Fusion custom pages, to the same or similar content on the new WordPress site.  If an invalid or unknown page number is entered, this redirect script will go to the default home page.

You could repeat the “if” statement and the “header” statement (lines 6-7) above as many times as desired.

Here are more details if you want the full scoop: The $_GET looks at the URL for a variable called page_id.  PHP-Fusion uses the url “viewpage.php” to show custom pages, but it’s always followed by the page number or page id as follows: viewpage.php?page_id=2.  The number 2 is used by PHP-Fusion as the key crestor cost price value of the CustomPage table and is thus used to build and display the proper content. You have to find the same content in WordPress, and then enter the URL you want to redirect to (in my example above, the custom-page=2 from PHP-Fusion is remapped to the WordPress post #43, and the custom-page=1 from PHP-Fusion to WordPress post #35.

Header() is a function built-in to the PHP language that sets the HTTP headers.  “Location” is one of those headers, and it’s also called a “server-side redirect”.  I think internally what happens is this.  The client browser requests viewpage.php, and we run our code.  But then, we tell the client’s browser that this page moved, and it (the browser) should request the new “location” page instead.  The browser handles all this automatically and silently, so the user doesn’t even notice – unless he is carefully watching the URL at the top of the browser.  Every time a browser request (via the “GET” command) a web page, it gets an http status back (such as 200 for okay, 301 for redirect, or 404 for page not found).

NOTE: If you name this script viewpage.php, then it will only handle redirect of custom pages. You can save a similar script for “articles.php” and instead of page_id, use article_id in the $_GET statement. I have not yet researched to the same degree how “news.php” is handled (whether there are really different news pages in PHP or just one big one).

We don’t want the user to get the 404 and be frustrated and leave our site.  Instead, the 301 permanent redirect let’s the browser display the new page of content, and our user/customer is happy.

The above, only works for old links to PHP-Fusion.  What if, however, you want to totally avoid the ugly 404 pages?  You can set this up on your “CPanel” pages, normally found at http://YourSite.com:2082 (2082 is the port number for CPanel), and CPanel is a “control panel” product installed on about 95% of all the typical PHP/MySQL hosting sites.

Scroll down or search for "error" on your CPanel

404 is the HTTP error status for “Page not found”

The next screen will allow you to select the desired error/status code, and you click “404″.

404 is the HTTP error/status code for "Page Not Found"

Then you type or paste the HTML in the screen below (substituting your domain name of course):

Now, if your user/customer types http://CMSTrainingVideos.com/someStupidPage.html, then he will automatically be directed to the home page.

Note also, that WordPress itself can catch bad page numbers.  For example, the link to a blog/article is something like: http://CMSTrainingVideos.com/?59.  But what if the user typed “?95″ instead of “?59″?   WordPress will catch this, and show the following screen, under your banner:

Later, we can talk about how to enhance that behavior.


Leave a Reply