ryan

How do I make ProcessWire display the 404 "page not found" page?

Recommended Posts

ProcessWire provides a 404 page that you can modify and customize like any other (and you'll see it in your Page List).

ProcessWire will automatically display that 404 page when someone attempts to access a non-existent URL.

If one of your templates is configured to show a 404 page when the user doesn't have access to a page (in Templates > Template > Advanced Settings), then it'll show your 404 page in that instance as well.

There may be instances where you want to trigger 404 pages on your own from your templates. To do this, use the following PHP snippet:

throw new PageNotFoundException(); 

Once you do that, processing of your template will stop and ProcessWire will send a 404 header and display the default 404 page instead (thereby passing control to your 404 template).

Please note: you should throw the PageNotFoundException before outputting any content in your template.

Share this post


Link to post
Share on other sites

Just a couple of notes - firstly, for anyone reading into throwing 404s in ProcessWire the preferred (as in used more in code samples here) way of doing this now seems to be this:

throw new Wire404Exception();

Also, I tried using an include file at the top of my template called global.inc that does some global bits and pieces and I noticed that it seems impossible to throw Wire404Exception in an included file - it amusingly throws an exception :D

  • Like 2

Share this post


Link to post
Share on other sites
Also, I tried using an include file at the top of my template called global.inc that does some global bits and pieces and I noticed that it seems impossible to throw Wire404Exception in an included file - it amusingly throws an exception

How do you duplicate that? I tried to duplicate but couldn't.

Here's the test I ran in my /site/templates/home.php:

<?php
include("./test.inc"); 

Here is /site/templates/test.inc:

<?php
throw new Wire404Exception(); 

Result was the 404 page. 

Share this post


Link to post
Share on other sites

I can confirm that it works in your example, just not when I include it in head.inc - I get this error:

2013-03-31 17:47:32    guest    http://www.mysite.co.uk/dev/http404/    Error:     Exception:  (in /home/mysite/public_html/dev/site/templates/head.inc line 2)

It's peculiar I know since the filename should make no difference.

I then renamed my default head.inc to test.inc and updated the code in the homepage template and it throws a 404, renamed it back to head.inc and changed the include in home.php and it logs the error message above again.

Share this post


Link to post
Share on other sites

Actually filename would make a difference here. The template used by the 404 page also includes head.inc. So head.inc gets included twice. If there are any function definitions (that get defined twice) that could trigger an exception. Also if the 404 is getting thrown without some kind of if() statement to know when to throw it, then it would be an infinite loop as the homepage throws it, then the 404 page throws it, and on and on. 

Share this post


Link to post
Share on other sites

No need for double redirects i think, just define it in your site/config.php:

$config->http404PageID = 27;

This line is from wire/config.php. Setting it in site/config.php should override. Just replace '27' with the id of any page you desire.

Untested and maybe i'm overlooking an obvious drawback but this should work.

  • Like 3

Share this post


Link to post
Share on other sites

404 pages serve an important purpose from an SEO standpoint. So if this is a public facing site, I'd recommend maintaining a proper 404 page rather than just redirecting to or displaying the homepage. You could always have your 404 page link to your homepage or even meta redirect to it after a few seconds. 

  • Like 5

Share this post


Link to post
Share on other sites

Hi Guys,

Am using the delayed output approach to the template structure and when I try:

throw new PageNotFoundException();

I get:

Error: Exception: (in /path/to/files/_main.php line 58)

#0 /path/to/files/wire/core/TemplateFile.php(170): require()
#1 [internal function]: TemplateFile->___render()
#2 /path/to/files/wire/core/Wire.php(389): call_user_func_array(Array, Array)
#3 /path/to/files/wire/core/Wire.php(344): Wire->runHooks('render', Array)
#4 /path/to/files/wire/modules/PageRender.module(356): Wire->__call('render', Array)
#5 /path/to/files/wire/modules/PageRender.module(356): TemplateFile->render()
#6 [internal function]: PageRender->___renderPage(Object(HookEvent))
#7 /path/to/files/wire/core/Wire.php(389): call_user_func_array(Array, Array)
#8 /path/to/files/wire/core/Wire.php(344): Wire->runHooks('renderPage', Array)
#9 /home/

What have I done wrong? Any suggestions would be sweet.

Thanks

Share this post


Link to post
Share on other sites

As noted above by Pete (2nd post), what you are actually looking for now is:

throw new Wire404Exception();

Does that work for you, or is there something else going on still?

  • Like 3

Share this post


Link to post
Share on other sites

I can't get the 404 working in a template or bootstrapped script :

<?php namespace ProcessWire;

include_once(__DIR__ . '/../index.php');

if(!$config->ajax || !$user->isGuest()) {
    throw new Wire404Exception();
}

 

as @Pete already said, it amusingly throws an exception on my install. I am on PW 3.0.88 but was not working on 3.086 either.

What I can check ?

If it help, I have TemplateEngineFactory installed and I think its the main cause. I might have posted on the module's thread...

 

Quote

Fatal error: Uncaught ProcessWire\Wire404Exception in /www/sites/***/wwwroot/api/contact.php:14 Stack trace: #0 {main} thrown in /www/sites/***/wwwroot/api/contact.php on line 14

 

Share this post


Link to post
Share on other sites

@flydev - I have ran into the same issues with my sites that use TemplateEngineFactory with TWIG.

I had to do the following:

$session->redirect($pages->get($config->http404PageID)->httpurl());
throw new Wire404Exception();

Maybe you could post something in the TemplateEngineFactory topic?  Maybe @Wanze would know of a fix?

Share this post


Link to post
Share on other sites

Thanks you. 

it turns out that with TemplateEngineFactory it works - at least for me - in templates, the 404 is fired, but not in bootstrapped script 😬 It is your case ?

And I ended up testing to throw a 404 from a bootstrapped script on two others websites which don't have TemplateEngineFactory and also on a fresh install and it doesn't work, I think we can dismiss the module.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Recently Browsing   0 members

    No registered users viewing this page.