Custom 404 Error Page with Permalinks plugin

5 posts by 3 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: July 4, 2018   (RSS)

By gversion - June 25, 2018

Hello,

I am trying to customise the 404 error page used by the permalinks plugin.

If I uncomment the variable $alternate404Url and redirect users to my custom page (e.g. 404.php) then when I check the headers of a non existent page using an online tool (such as https://monitorbacklinks.com/seo-tools/http-header-status-check) I receive a 302 found header.

My custom 404 error page is using the following code

<?php
// load viewer library
$libraryPath = 'cmsb/lib/viewer_functions.php';
$dirsToCheck = array('/opt/bitnami/apache2/htdocs/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

header("HTTP/1.0 404 Not Found");
$REDIRECT_URL = _permalink_getRedirectUrl();

$title = '404 Error: Page Not Found ';
require('header.php');
?>

<div id="main-content" class="row">
<div class="wrapper">
<div class="col-xs-12">
<h1 class="search-results"><strong>Page Not Found</strong></h1>
</div>
<div class="col-xs-12">
<hr class="visible-xs visible-sm" />
</div>
<div class="col-xs-12">
<div class="alert alert-danger">The requested URL was not found on this server.</div>
</div>
</div> <!-- /wrapper -->
</div> <!-- /main-content -->


<?php require('footer.php'); ?>

I thought by specifying header("HTTP/1.0 404 Not Found"); I would return a 404 header. If I check the headers of my custom 404 page then they do return as a 404 but if I check the headers of a non existent page then it's a 302. I guess because it is redirecting...??

Can someone please help?

Thank you,

Greg

By leo - June 25, 2018

Hi Greg,

Yes, it is because of the redirecting. I would recommend you modify the 404 page to add the libraries you need.

Thanks,

Leo - PHP Programmer (in training)
interactivetools.com

By gversion - June 28, 2018

Thanks, Leo.

Could the plugin code not be changed slightly as follows:

  $alternate404Url = "http://www.mydomain.com/404.php";
  if (@$alternate404Url) {
    dieWith404(file_get_contents($alternate404Url));
    exit;
  }

Would that still redirect and then correctly send the 404 headers?

Thanks,

Greg

By gregThomas - July 2, 2018

Hey Greg,

That would work, but I'd recommend including the file path to the file instead of using the URL:

  $alternate404Url = "/path/to/404.php";
  if (@$alternate404Url) {
    dieWith404(file_get_contents($alternate404Url));
    exit;
  }

This is because there is a small chance that the file_get_contents to the URL might cause a 404, which could potentially cause an infinite loop.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com