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


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 I receive a 302 found header.

My custom 404 error page is using the following code

// 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 ';

<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 class="col-xs-12">
<hr class="visible-xs visible-sm" />
<div class="col-xs-12">
<div class="alert alert-danger">The requested URL was not found on this server.</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,


By gversion - June 28, 2018

Thanks, Leo.

Could the plugin code not be changed slightly as follows:

  $alternate404Url = "";
  if (@$alternate404Url) {

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



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) {

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.



Greg Thomas

PHP Programmer -

By gversion - July 4, 2018

Hi Greg,

Very good to know.

Thanks so much for the help!