kostianev Posted January 12, 2016 Share Posted January 12, 2016 Hello, I want to redirect my error 404 page to the home page. I tried to change this .htaccess line #If rewrite mod isn't enabled ErrorDocument 404 /index.php but nothing happen, the error page stay the same. I use prestashop 1.6.0.11 Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 Hi, Edit following file on your server. /controllers/front/PageNotFoundController.php Search for $this->setTemplate(_PS_THEME_DIR_.'404.tpl'); Now add below line of code above searched result. Tools::redirect(); Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Hello, I added the code and look like this: parent::initContent(); Tools::redirect(); $this->setTemplate(_PS_THEME_DIR_.'404.tpl'); } And when I type wrong result in URL address of my website get white blank page? Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 Which Prestashop version are you using? Also let enable your developer mode to check error on that blank screen. To enable developer mode. To enable developer mode, you need to edit defines.inc.php file in /config folder. Search define('_PS_MODE_DEV_', false); and replace it with define('_PS_MODE_DEV_', true); Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Here is the error on 404 page which I get: Warning: Missing argument 1 for ToolsCore::redirect(), called in /var/www/clients/client1/web1/web/controllers/front/PageNotFoundController.php on line 113 and defined in /var/www/clients/client1/web1/web/classes/Tools.php on line 77Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 82Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 82Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 84Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 86Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 93 Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 Try below code Tools::redirect('index.php'); It should definitely fix your issue. Regards, Knowband Plugins Team Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Okay, I do it and now again got White Blank Page without any error msg? Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 Hi, Which Prestashop Version are you using? Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Prestashop 1.6.0.11 Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 We checked it in same version and Tools::redirect(); is working fine. Without looking into your website code, it is difficult to say anything now. Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Okay, I can give you login to view? This is the website link Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 Can you share PHP file that you have modified? Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Hello, here is the file/ PageNotFoundController.php Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 13, 2016 Share Posted January 13, 2016 We have checked code, you have edited it correctly. Just you a message, please check. Link to comment Share on other sites More sharing options...
kostianev Posted January 13, 2016 Author Share Posted January 13, 2016 Okay I replayed Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 14, 2016 Share Posted January 14, 2016 You needed to place Tools::redirect(); in the starting of init function See below: public function initContent() { Tools::redirect(); 1 Link to comment Share on other sites More sharing options...
kostianev Posted January 14, 2016 Author Share Posted January 14, 2016 Thank you, this in which file? Link to comment Share on other sites More sharing options...
Knowband Plugins Posted January 14, 2016 Share Posted January 14, 2016 In the same file, /controllers/front/PageNotFoundController.php Link to comment Share on other sites More sharing options...
kostianev Posted January 14, 2016 Author Share Posted January 14, 2016 Thank you for the help mate, now is working fine as I want to be! Link to comment Share on other sites More sharing options...
caravntrader Posted February 10, 2016 Share Posted February 10, 2016 Hello kostianiev, can you please poste your /controllers/front/PageNotFoundController.php? i am trying to do the same redirection but it wont work! thank you Link to comment Share on other sites More sharing options...
kostianev Posted February 10, 2016 Author Share Posted February 10, 2016 Here you are: <?php /* * 2007-2015 PrestaShop * * NOTICE OF LICENSE * * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/osl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to [email protected] so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to http://www.prestashop.com for more information. * * @author PrestaShop SA <[email protected]> * @copyright 2007-2015 PrestaShop SA * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class PageNotFoundControllerCore extends FrontController { public $php_self = '404'; public $page_name = 'pagenotfound'; public $ssl = true; /** * Assign template vars related to page content * @see FrontController::initContent() */ public function initContent() { Tools::redirect(); header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); if (preg_match('/\.(gif|jpe?g|png|ico)$/i', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) { $this->context->cookie->disallowWriting(); if (!isset($_SERVER['REDIRECT_URL'])) { $_SERVER['REDIRECT_URL'] = ''; if (preg_match('@^'.__PS_BASE_URI__.'([0-9]+)\-([_a-zA-Z0-9-]+)(/[_a-zA-Z0-9-]+)?\.jpg$@', $_SERVER['REQUEST_URI'], $matches)) $_SERVER['REDIRECT_URL'] = __PS_BASE_URI__.'p/'.Image::getImgFolderStatic($matches[0]).'/'.$matches[0].'-'.$matches[1].'.jpg'; } if (preg_match('#/p[0-9/]*/([0-9]+)\-([_a-zA-Z]*)\.(png|jpe?g|gif)$#', $_SERVER['REDIRECT_URL'], $matches)) { // Backward compatibility since we suffixed the template image with _default if (Tools::strtolower(substr($matches[2], -8)) != '_default') { header('Location: '.$this->context->link->getImageLink('', $matches[1], $matches[2]), true, 302); exit; } else { $image_type = ImageType::getByNameNType($matches[2], 'products'); if ($image_type && count($image_type)) { $root = _PS_PROD_IMG_DIR_; $folder = Image::getImgFolderStatic($matches[1]); $file = $matches[1]; $ext = '.'.$matches[3]; if (file_exists($root.$folder.$file.$ext)) if (ImageManager::resize($root.$folder.$file.$ext, $root.$folder.$file.'-'.$matches[2].$ext, (int)$image_type['width'], (int)$image_type['height'])) { header('HTTP/1.1 200 Found'); header('Status: 200 Found'); header('Content-Type: image/jpg'); readfile($root.$folder.$file.'-'.$matches[2].$ext); exit; } } } } elseif (preg_match('#/c/([0-9]+)\-([_a-zA-Z]*)\.(png|jpe?g|gif)$#', $_SERVER['REDIRECT_URL'], $matches)) { $image_type = ImageType::getByNameNType($matches[2], 'categories'); if ($image_type && count($image_type)) { $root = _PS_CAT_IMG_DIR_; $file = $matches[1]; $ext = '.'.$matches[3]; if (file_exists($root.$file.$ext)) if (ImageManager::resize($root.$file.$ext, $root.$file.'-'.$matches[2].$ext, (int)$image_type['width'], (int)$image_type['height'])) { header('HTTP/1.1 200 Found'); header('Status: 200 Found'); header('Content-Type: image/jpg'); readfile($root.$file.'-'.$matches[2].$ext); exit; } } } header('Content-Type: image/gif'); readfile(_PS_IMG_DIR_.'404.gif'); exit; } elseif (in_array(Tools::strtolower(substr($_SERVER['REQUEST_URI'], -3)), array('.js', 'css'))) { $this->context->cookie->disallowWriting(); exit; } parent::initContent(); $this->setTemplate(_PS_THEME_DIR_.'404.tpl'); } protected function canonicalRedirection($canonical_url = '') { // 404 - no need to redirect to the canonical url } protected function sslRedirection() { // 404 - no need to redirect } } 1 Link to comment Share on other sites More sharing options...
caravntrader Posted February 10, 2016 Share Posted February 10, 2016 Thanks my dear. work fine any solution to redirect deleted products? Link to comment Share on other sites More sharing options...
Knowband Plugins Posted February 11, 2016 Share Posted February 11, 2016 Hi caravntrader, Please find attached file, in case this method don't work for you, you can contact us to get it done. Since it is a small patch, we will do it for FREE PageNotFoundController.php.zip 1 Link to comment Share on other sites More sharing options...
dorje Posted April 24, 2016 Share Posted April 24, 2016 Hi knowband I have more that 600 url which is 404 . all these are disabled products, old modules, old blog posts etc. i want all of them to be redirected to homepage or to related categories < for disabled product > Can u please help me on this Link to comment Share on other sites More sharing options...
Knowband Plugins Posted April 25, 2016 Share Posted April 25, 2016 Hi knowband I have more that 600 url which is 404 . all these are disabled products, old modules, old blog posts etc. i want all of them to be redirected to homepage or to related categories < for disabled product > Can u please help me on this Above solution should also work for you. Try editing PageNotFoundController.php file, search for Tools::redirect(); and add below line after it. Tools::redirect(); // added code to redirect customer to homepage rather than to 404 page. Let us know if you need further assistance. Link to comment Share on other sites More sharing options...
SmartPlugs Posted April 25, 2016 Share Posted April 25, 2016 Hi Dorje, I developped a module that automaticaly add a redirection on product or category deletion (for products, categories or blog posts you will delete in the future). For the products you already have deleted, it's more experimental but you could configure it to add a redirection (unactive) when a 404 error occurs and then choose the ones you want to activate (because the url looks like a product one or because the url has been requested many times for example). Hope it helps, Yann Link to comment Share on other sites More sharing options...
dorje Posted April 26, 2016 Share Posted April 26, 2016 @knowband yeah above code works fine for some urls but not for all. IT doesnt work for disabled products. Also the redirects are 302 not 301 , so i want the 301 redirection. i did check the code and replace the 302 to 301 but still the same redirection code shows which is 302 ex check the redirection code for these url http://www.kathmanduclothing.com/module/mailalerts/actions?process=add http://www.kathmanduclothing.com/en/wholesale-tops-for-women/155-ssd-701-.html "if (Tools::strtolower(substr($matches[2], -8)) != '_default') { header('Location: '.$this->context->link->getImageLink('', $matches[1], $matches[2]), true, 301); exit; Also some of the disabled product urls which does not redirects. Please check them ex http://www.kathmanduclothing.com/woolen-hats/22-gb-64-.html shows 404 http://www.kathmanduclothing.com/nepali-scarves/50-sh0680-.html and too many but in 1.6 x we can redirect it manually one by one to another related products which is very time consuming, so i just want to redirect all disable products to either their categories or to homepage. Any help would be very much appreciated. Link to comment Share on other sites More sharing options...
fransjaeger Posted July 9, 2016 Share Posted July 9, 2016 I did it this way: File: /override/controllers/front/PageNotFoundController.php Code: class PageNotFoundController extends PageNotFoundControllerCore { public function initContent() { parent::initContent(); header('HTTP/1.1 301 Moved Permanently'); Tools::redirect('index.php'); } } Dont forget to delete cache/your class_index.php file after you put file in override folder. A new file will automatically be generated next time you visit site Link to comment Share on other sites More sharing options...
dorje Posted July 10, 2016 Share Posted July 10, 2016 hi ransjaeger i tried your solution but not working at all. my store version is 1.6.14 Link to comment Share on other sites More sharing options...
lovemyseo Posted November 26, 2016 Share Posted November 26, 2016 Hi caravntrader, Please find attached file, in case this method don't work for you, you can contact us to get it done. Since it is a small patch, we will do it for FREE Does this work with prestashop 1.6.1.6? Link to comment Share on other sites More sharing options...
Blessy_Preferred Posted May 25, 2018 Share Posted May 25, 2018 On 1/13/2016 at 10:39 PM, Knowband Plugins said: You needed to place Tools::redirect(); in the starting of init function See below: public function initContent() { Tools::redirect(); Works on PS 1.7.2.4 Thank you! Link to comment Share on other sites More sharing options...
Ebersol Posted January 28, 2020 Share Posted January 28, 2020 Prestashop 1.6.0.6 Can you please show me how to redirect 404 to the url? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now