Search code examples
phphttp-redirecthttp-status-code-404prestashopprestashop-1.6

How to disable prestashop's page-not-found redirect?


I have a Prestashop 1.6 installed on a VPS hosting and all is working well with it, except that when I try to reach a page that doesn't exist, instead of serving the 404 page of the template, a 301 redirect is returned and the client is redirected to index.php?controller=page-not-found where the 404 template is shown.

This is not working for me due to SEO reasons. How do I disable this redirect?

Thanks


Solution

  • Turns out the default behavior of prestashop is to redirect. It was not a override. Anyway - I found a way to achieve what I wanted to do anyway.

    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. 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.