Jump to content

I Want To Redirect My 404 Error Page To The Home Page


Recommended Posts

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

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 77

Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 82

Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 82

Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 84

Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 86

Notice: Undefined variable: url in /var/www/clients/client1/web1/web/classes/Tools.php on line 93

Link to comment
Share on other sites

  • 4 weeks later...

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

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

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

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

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

@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

  • 2 months later...

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

  • 4 months later...
  • 1 year later...
  • 1 year later...

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