Check 404 page at template index.php

Be informed that this forum is not an official support forum for Joomla! 4.0. Any issues regarding Joomla! 4.0 must be reported at https://issues.joomla.org/.

Joomla 4.0 is still in Beta stage. This forum should be used for sharing information about Joomla! 4.0.
Post Reply
Overtone
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Oct 07, 2019 2:37 pm

Check 404 page at template index.php

Post by Overtone » Thu Jan 21, 2021 9:03 pm

I have changed the error.php through adding these lines:

Code: Select all

if (($this->error->getCode()) == '404') {
    header("HTTP/1.0 404 Not Found");
    echo file_get_contents(JURI::root().'index.php');
    exit;
}
This works perfect and I got a status of 404 if I open a broken link. But how can I check in the template index.php that the status code is 404? If I add these lines I get a 500 error:

Code: Select all

if (($this->error->getCode()) == '404') { echo "wrong page blabla" }
Have you any idea?

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 22965
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Check 404 page at template index.php

Post by pe7er » Fri Jan 22, 2021 11:49 am

What is the exact 500 error? Could you change the PHP error reporting of your site?

Your code misses a semicolon behind the echo
it should be:

Code: Select all

if (($this->error->getCode()) == '404') { echo "wrong page blabla"; }
btw: why would you put a mesage in your template?
You could also create a 404 error article in your site, and a menu item linked to that article with alias "error"
and load that menu item / article using:

Code: Select all

if ($this->error->getCode() == '404')
{
    use Joomla\CMS\Uri\Uri;

    header("HTTP/1.0 404 Not Found");
    echo file_get_contents(URI::root() . '/error';
    exit;
}
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Nederland
The best website: https://the-best-website.com

Overtone
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Oct 07, 2019 2:37 pm

Re: Check 404 page at template index.php

Post by Overtone » Fri Jan 22, 2021 5:34 pm

The error I got at the frontpage goes to the line in the error.php with the code

Code: Select all

echo file_get_contents(JURI::root().'index.php');
The semikolon is only missing here in the forum thread!

I would like to redirect to the index, because there are a lot of self made modules which I would not like to duplicate for the error.php.

Overtone
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Oct 07, 2019 2:37 pm

Re: Check 404 page at template index.php

Post by Overtone » Fri Jan 22, 2021 5:37 pm

I just saw that it is only a warning, but the rest of the page don't build up. I see only:

Code: Select all

 Warning: file_get_contents(https://www.freelance-market.de/index.php): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /homepages/21/d89776460/htdocs/joomla4/templates/cassiopeia/error.php on line 61

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 27543
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Check 404 page at template index.php

Post by Per Yngve Berg » Fri Jan 22, 2021 8:23 pm

Mod. Note: Relocated the topic from J3 to J4 forum.

Overtone
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Oct 07, 2019 2:37 pm

Re: Check 404 page at template index.php

Post by Overtone » Sat Jan 23, 2021 4:24 pm

Thank you Per Yngve Berg. You are absolutely right.

Overtone
Joomla! Apprentice
Joomla! Apprentice
Posts: 17
Joined: Mon Oct 07, 2019 2:37 pm

Re: Check 404 page at template index.php

Post by Overtone » Mon Jan 25, 2021 4:40 pm

I found out something else. In the template cassiopeia error.php this is working:

Code: Select all

if (($this->error->getCode()) == '404') { echo "wrong page blabla"; }
But in the template cassiopeia index.php the same code shows:

Code: Select all

Call to a member function getCode() on null


Post Reply

Return to “Joomla! 4 Related”