Jump to content

Displaying 404 page without redirect


Recommended Posts

Hi guys,

 

New to the forum, needing a bit of a help with a prestashop installation.

I spent some time searching around for the answer to my question, but was unable to find a solution, so I hope you guys with your big brains can help me out :)

 

The default way of handling a missing page in prestashop 1.6, at least as far as I understand is this:

 

When a user tries to accesss a page that doesn't exist a 301 redirect is returned, and the client is sent to the 404 page.

 

What I want to achieve is to skip the redirect and serve the 404 page automatically. Headers and content both.

 

Do you guys think you can point me in the general direction of how to achieve that?

I am a programmer so no need to shy-away from the coding part.

 

Thank you very much,

Brad

Link to comment
Share on other sites

After some time struggling with that - I came up with a retarded solution, that will do for now.

I am sharing it here, because I sure would have appreciated finding it few days ago, but be warned - it is retarded to the point of no return :D

Not recommended if you can come up with something else.

 

What I did was this - I found the CategoryController.php file in the controllers/front/ folder.
There should  be this row:

 

if (!$this->category->active)

 

In it I make a php file_get_contents request to the 404.html page of the website, and serve it.

Afterwards I terminate the php script with an exit(); command.

 

This way the server sends out the 404.html content, without redirecting.

Here is the full code: 

header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
 
echo file_get_contents('https://website.com/404/'
false,
stream_context_create(
array(
'http' => array(
'ignore_errors' => true
)
)
)
);
exit();
 

 

Don't forget that if the controller is overridden it might not work properly.

Link to comment
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
×
×
  • Create New...