skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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.


  2. I’ve done this in overrided Tools controller

    public static function redirectLink($url)
    {

     $init_url=$url;
    
      if (!preg_match('@^https?://@i', $url)) {
          if (strpos($url, __PS_BASE_URI__) !== false && strpos($url, __PS_BASE_URI__) == 0) {
              $url = substr($url, strlen(__PS_BASE_URI__));
          }
          if (strpos($url, 'index.php?controller=') !== false && strpos($url, 'index.php/') == 0) {
              $url = substr($url, strlen('index.php?controller='));
          }
          $explode = explode('?', $url);
          $url = Context::getContext()->link->getPageLink($explode[0]);
          if (isset($explode[1])) {
              $url .= '?'.$explode[1];
          }
      }
    
    
    if ((string)trim($init_url)==='404') {
          header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
    
          header('Status: 404 Not Found');
          echo file_get_contents($url, false,
                stream_context_create(
                  array('http' =>  array('ignore_errors' => true,
                                   ),
                  )
                )
              );
    
          exit();
        } else {
    
                header("HTTP/1.1 301 Moved");
                header('Location: '.$url);
                exit();
    
        }
    

    }

    Login or Signup to reply.
  3. A global solution for this (so you don’t have to go controller by controller) is going straight to the -> classes/Dispatcher.php
    In there you will find the dispatch function.
    You can do something like ->

    if ($this->controller == $this->controller_not_found) {
    //your code here
    }
    

    before the line where the controller is going to be executed

    $controller->run();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search