How to redirect to another URL instead of generating error message?

4 posts by 2 authors in: Forums > CMS Builder
Last Post: June 13, 2017   (RSS)

By furcat - June 7, 2017

I have the following code in a file for one of my websites:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/html/rossinimanagement.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($case_studiesRecords, $case_studiesMetaData) = getRecords(array(
'tableName' => 'case_studies',
));

list($first_pageRecords, $first_pageMetaData) = getRecords(array(
'tableName' => 'first_page',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$first_pageRecord = @$first_pageRecords[0]; // get first record

// show error message if no matching record is found
if (!$first_pageRecord) { dieWith404("Record not found!"); }

?>

I want to direct the viewer to another page if there is an error and/or if the record is not found instead of generating an error message. Is that possible, and if so, how do I do it?

By Damon - June 7, 2017 - edited: June 7, 2017

Hi,

Here is the code to redirect to another page instead of displaying the "Record not found" message.

Change this code:

if (!$first_pageRecord) { dieWith404("Record not found!"); }

to this:

if (!$first_pageRecord) { header('Location: http://site.com/404.php'); exit; }

And add the change the URL to the page you want to redirect to.

That's it. Let me know if you have any questions or need anything else.

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By furcat - June 13, 2017

I gave it a try and I'm not getting what I'm wanting. I am still having headaches with the Google Ad string that's being passed to the URL from the advertising company my customer hired. You helped me with that last month. I was thinking if something failed, instead of getting "record not found" then at least the viewer could view the webpage. Maybe even the ad may still convert; I don't know.

I have the following: 

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/html/rossinimanagement.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/var/www/html/rossinimanagement.com/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records
list($awardsRecords, $awardsMetaData) = getRecords(array(
'tableName' => 'awards',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$awardsRecord = @$awardsRecords[0]; // get first record

// show error message if no matching record is found
if (!$first_pageRecord) { header('Location: http://kuecker.com/awards-test.php'); exit; }

?>

I have a test file out there with the ad info tacked onto the URL where you can see the error message I received: http://kuecker.com/awards-test.php/?keyword=%2Bkuecker%20%2Blogistics&gclid=CIL55sDS9NMCFdC4wAodApULLQ

Am I just not able to "send" it to the same URL? Circular reasoning, I know, but, just looking for some options.

Thank you.

By Damon - June 13, 2017

Hi,

I looks like redirecting back to the same page is causing issues.

Instead of displaying "record not found" or redirecting to the same page, you could display some content.

So, instead of having either this code:

if (!$first_pageRecord) { dieWith404("Record not found!"); } 

or this code:

if (!$first_pageRecord) { header('Location: http://kuecker.com/awards-test.php'); exit; }

You can display other content anywhere in the page with this code:

<?php if (!$first_pageRecord) : ?>

   Add content here..

<?php endif; ?>

Can you try this?

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/