Jump to content

How do I redirect 404 to my homepage?


cutecat

Recommended Posts

this is in the htaccess

 

default is this

 

#If rewrite mod isn't enabled

ErrorDocument 404 /index.php?controller=404

 

i changed to this

ErrorDocument 404 /index.php

and it still shows 404 page.

 

there is also a 404.php in my root too. but I am not sure how to edit.

 

Pls advise

Link to comment
Share on other sites

the ErrorDocument 404 /prestashop/404.php in .htacess should handle your 404 properly but if you want to redirect your all 404 pages then you should use this code in htacess instead of the above one

ErrorDocument 404 /

But i personally dont recommend it because it will redirect all 404 pages to your homepage and it will not help you to track the site original 404 issues.

Link to comment
Share on other sites

the ErrorDocument 404 /prestashop/404.php in .htacess should handle your 404 properly but if you want to redirect your all 404 pages then you should use this code in htacess instead of the above one

ErrorDocument 404 /

But i personally dont recommend it because it will redirect all 404 pages to your homepage and it will not help you to track the site original 404 issues.

 

i had some of my customers who are acting smarting smart by removing the product name to do to the category.

which gave them an error page

 

also I have placed ErrorDocument 404 /index.php and it does not work either

Link to comment
Share on other sites

I have the same problem ... want to redirect all 404 errors to Homepage ... solution ErrorDocument 404 / dosent work.

 

Possible solution:

set friendlly url for 404 and then redirect this url to homepage

 

you have to put this code in htaccess:

 

# Redirect

Redirect 301 /page-not-found http://www.homepage.com

 

In my case friendly url is working for all pages except 404 error pages, so i can not do that.

Edited by Frenkie (see edit history)
Link to comment
Share on other sites

No Dorje that is not the Idea. I want that everyone, even those who type wrong address, come on page with marketing content not on ugly 404 error. This is also double work. Why not just redirect all 404 errors to the homepage?

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

Why not edit the 404 template file in the theme to do a javascript redirect?

That way you will still be able to track 404 errors and at the same time serve your visitors the homepage or any marketing page you want.

Just add:

<script type="text/javascript">
<!--
window.location.replace("yoururl");
//-->
</script>

Edited by scorpionsworld (see edit history)
  • Like 2
Link to comment
Share on other sites

Why not edit the 404 template file in the theme to do a javascript redirect?

That way you will still be able to track 404 errors and at the same time serve your visitors the homepage or any marketing page you want.

Just add:

<script type="text/javascript">
<!--
window.location.replace("yoururl");
//-->
</script>

 

this is what i had done. but it still does not show my homepage

 

<?php
/*
* 2007-2013 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-2013 PrestaShop SA
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

require_once(dirname(__FILE__).'/config/config.inc.php');
Tools::displayFileAsDeprecated();
/* Controller::getController('PageNotFoundController')->run();
*/
<script type="text/javascript">
<!--
window.location.replace("http://123.com/");
//-->
</script>

Link to comment
Share on other sites

this is what i had done. but it still does not show my homepage

 

<?php
/*
* 2007-2013 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-2013 PrestaShop SA
*  @license	http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

require_once(dirname(__FILE__).'/config/config.inc.php');
Tools::displayFileAsDeprecated();
/* Controller::getController('PageNotFoundController')->run();
*/
<script type="text/javascript">
<!--
window.location.replace("http://123.com/");
//-->
</script>

That's because you placed the javascript in the wrong file. PHP is not able to execute Javascript, only parse PHP code before sending it to the browser, who in turn does know how to execute Javascript.

And don't comment out the getController function, it will kill the 404 error tracking and parsing the template.

 

Why not edit the 404 template file in the theme to do a javascript redirect?

Edited by scorpionsworld (see edit history)
  • Like 1
Link to comment
Share on other sites

you should edit the

pagenotfoundcontroller.php

in controller folder

 

open your controller folder

and edit

pagenotfoundcontroller.php

 

just copy and paste into pagenotfoundcontroller.php

and change the http://tegalgubug.net to your homepage

 

 

<?php

/*

* 2007-2013 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.or...ses/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-2013 PrestaShop SA

* @license http://opensource.or...ses/osl-3.0.php Open Software License (OSL 3.0)

* International Registered Trademark & Property of PrestaShop SA

*/

 

class PageNotFoundControllerCore extends FrontController

{ /*ini sudah diganti */

public $php_self = '404';

public $page_name = 'pagenotfound';

 

/**

* Assign template vars related to page content

* @see FrontController::initContent()

*/

public function initContent()

{

/*here you should change your homepage */

header("Location: http://tegalgubug.net/");

 

if (in_array(Tools::strtolower(substr($_SERVER['REQUEST_URI'], -3)), array('png', 'jpg', 'gif')))

{

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')))

exit;

 

parent::initContent();

 

$this->setTemplate(_PS_THEME_DIR_.'404.tpl');

}

 

public function canonicalRedirection($canonical_url = '')

{

// 404 - no need to redirect to the canonical url

}

}

 

 

 

 

----- toko obat herbal |obat alami------

Edited by siblibusro (see edit history)
  • Like 6
Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...

I have new visitors from 404 pages. Instead of just redirecting them, I offer a special voucher at that 404 page to attract them searching my store, instead of 'forcing' them to view things they are not originally looking for.

 

do you all think it is a good thing to do?

Link to comment
Share on other sites

  • 1 month later...

you should edit the

pagenotfoundcontroller.php

in controller folder

 

open your controller folder

and edit

pagenotfoundcontroller.php

 

just copy and paste into pagenotfoundcontroller.php

and change the http://tegalgubug.net to your homepage

 

 

<?php

/*

* 2007-2013 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.or...ses/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-2013 PrestaShop SA

* @license http://opensource.or...ses/osl-3.0.php Open Software License (OSL 3.0)

* International Registered Trademark & Property of PrestaShop SA

*/

 

class PageNotFoundControllerCore extends FrontController

{ /*ini sudah diganti */

public $php_self = '404';

public $page_name = 'pagenotfound';

 

/**

* Assign template vars related to page content

* @see FrontController::initContent()

*/

public function initContent()

{

/*here you should change your homepage */

header("Location: http://tegalgubug.net/");

 

if (in_array(Tools::strtolower(substr($_SERVER['REQUEST_URI'], -3)), array('png', 'jpg', 'gif')))

{

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')))

exit;

 

parent::initContent();

 

$this->setTemplate(_PS_THEME_DIR_.'404.tpl');

}

 

public function canonicalRedirection($canonical_url = '')

{

// 404 - no need to redirect to the canonical url

}

}

 

 

 

 

----- toko obat herbal |obat alami------

God bless you my friend!!!!Thank you so much for this solution.I was looking for this last days.Work like a charm :).thank you

Link to comment
Share on other sites

  • 4 weeks later...

That's because you placed the javascript in the wrong file. PHP is not able to execute Javascript, only parse PHP code before sending it to the browser, who in turn does know how to execute Javascript.

And don't comment out the getController function, it will kill the 404 error tracking and parsing the template.

worked for me, thanks for the tip!

Link to comment
Share on other sites

Dear friends.

 

This website has a great way. Its 404 page automatically redirect to home page in 30 seconds.

 

Try this link http://www.naaptol.com/thisiswronglink

 

I think that is most suitable way.

 

How we create our Prestashop 404 page with that method.

 

If anyone find a way, Please update this topic. It will help to everyone.

Just check the source of this 404 page. The redirect is not in 30 seconds, but 5 seconds.

 

<META HTTP-EQUIV="refresh" content="5; url=/">

 

Not suitable for Prestashop implementation.

Besides, wether you think this way is most suitable is irrelevant.

It's up to every shop owner to decide what best suites his shop or purpose of the 404 landing page.

Link to comment
Share on other sites

  • 1 month later...

You can use Location Header function to redirect a page. This script works when we have not send any php or html command to out put any thing in the browser. Care is to be taken not to send any other thing to the browser after the header redirection using location command. Here is the code.

 

<?php
header("HTTP/1.1 301 Moved Permanently");
header ("Location: your homepage here");
exit;
?>

Simply drop the code above at the top of the controllers/front/PageNotFoundController.php file

Link to comment
Share on other sites

  • 10 months later...
  • 5 weeks later...
  • 2 months later...

What I should edit on this "pagenotfoundcpntroller.php" file to redirect 404 error to fronpage?

 

Website: seksituotteet.fi

Prestashop 1.6.0.9

 

<?php
/*
* 2007-2014 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:
* 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-2014 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()
{
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')
Link to comment
Share on other sites

thx, but didn't work.

 

I add this line to end of .htaccess file

RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
Link to comment
Share on other sites

I edit the code in PAGENOTFOUNDCONTROLLER.PHP file like this. And now Redirect works.

 

Orginal code was:

header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
 
I replace it with this:
 
 
Now page loading mark doesent stop at all. See image.
 
 

post-21451-0-01979400-1422777365_thumb.jpg

Edited by cille5 (see edit history)
Link to comment
Share on other sites

  • 2 months later...
  • 1 year later...

For 1.6.1.5

<?php
/*
* 2007-2016 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-2016 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 = 'pagenotfound';
    public $page_name = 'pagenotfound';
    public $ssl = true;

    /**
     * Assign template vars related to page content
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        //  header('HTTP/1.1 404 Not Found');
        //  header('Status: 404 Not Found');
	header("Location: http://prestashark.eu/");
        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
    }
}

Just found this lines

 

// header('HTTP/1.1 404 Not Found');
// header('Status: 404 Not Found');
header("Location: http://prestashark.eu/");

 

and insert yourdomain.com instead of prestashark.eu

 

thats all. works like a charm

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