logs00 Posted July 24, 2014 Share Posted July 24, 2014 Bonjour,Afin d'améliorer le SEO de notre site, j'aimerais savoir comment il est possible de modifier le comportement des pages inexistantes qui sont forcément redirigées en 301 vers une page d'erreur 404.Exemple : La page http://www.monsite.com/toto n'existe pas et est redirigée en 301 vers la page http://www.monsite.com/page-non-trouvee qui elle envoi un HTTP 404.Mon but serait de rester sur l'URL http://www.monsite.com/toto mais en envoyant un header HTTP 404 et affichant la page d'erreur 404.Cela éviterait d'avoir un grand nombre de redirection 301 vue par Google.Certains site le fond déjà (http://www.prestashop.com/toto ), et j'aurais voulu savoir comment le faire sous Prestashop ?Merci à vous,Logs PS : je tourne sur PS 1.5.2.0. Link to comment Share on other sites More sharing options...
Eolia Posted July 24, 2014 Share Posted July 24, 2014 (edited) Sur 1.5.6.2 je n'ai pas le pb ? (redirection 404 uniquement sans modif de l'url) Dans classes -> Dispatcher.php: /** * @var string Controller to use if found controller doesn't exist */ protected $controller_not_found = 'pagenotfound'; ... /** * Need to be instancied from getInstance() method */ protected function __construct() { $this->use_routes = (bool)Configuration::get('PS_REWRITING_SETTINGS'); // Select right front controller if (defined('_PS_ADMIN_DIR_')) { $this->front_controller = self::FC_ADMIN; $this->controller_not_found = 'adminnotfound'; $this->default_controller = 'adminhome'; } elseif (Tools::getValue('fc') == 'module') { $this->front_controller = self::FC_MODULE; $this->controller_not_found = 'pagenotfound'; $this->default_controller = 'default'; } else { $this->front_controller = self::FC_FRONT; $this->controller_not_found = 'pagenotfound'; $this->default_controller = 'index'; } $this->setRequestUri(); // Switch language if needed (only on front) if (in_array($this->front_controller, array(self::FC_FRONT, self::FC_MODULE))) Tools::switchLanguage(); if (Language::isMultiLanguageActivated()) $this->multilang_activated = true; $this->loadRoutes(); } ... Et dans controller -> front ->PageNotFoundController.php: <?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 */ class PageNotFoundControllerCore extends FrontController { public $php_self = '404'; public $page_name = 'pagenotfound'; /** * 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 (in_array(Tools::strtolower(substr($_SERVER['REQUEST_URI'], -3)), array('png', 'jpg', 'gif'))) { $this->context->cookie->disallowWriting(); if ((bool)Configuration::get('PS_REWRITING_SETTINGS')) preg_match('#([0-9]+)(\-[_a-zA-Z0-9-]*)?(-[0-9]+)?/(.+)\.(png|jpg|gif)$#', $_SERVER['REQUEST_URI'], $matches); if ((!isset($matches[2]) || empty($matches[2])) && !(bool)Configuration::get('PS_REWRITING_SETTINGS')) preg_match('#/([0-9]+)(\-[_a-zA-Z]*)\.(png|jpg|gif)$#', $_SERVER['REQUEST_URI'], $matches); if (is_array($matches) && !empty($matches[2]) && Tools::strtolower(substr($matches[2], -8)) != '_default' && is_numeric($matches[1])) { $matches[2] = substr($matches[2], 1, Tools::strlen($matches[2])).'_default'; if (!isset($matches[4])) $matches[4] = ''; header('Location: '.$this->context->link->getImageLink($matches[4], $matches[1], $matches[2]), true, 302); 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'); } public function canonicalRedirection($canonical_url = '') { // 404 - no need to redirect to the canonical url } } à voir comment l'adapter sur une 1.5.2 (modif ou remplacement) Edited July 24, 2014 by Eolia (see edit history) Link to comment Share on other sites More sharing options...
logs00 Posted July 24, 2014 Author Share Posted July 24, 2014 (edited) Et bah je dit un grand MERCI !!!Réponse rapide et fonctionnelle :-) Pour l'adapter à PS 1.5.2 je n'ai eu juste qu'à overrider le controller PageNotFoundController avec votre code.Tout est ok niveau redirection maintenant et mes URLs restent bien comme ont les appelles. Je re-posterais si je vois que ça à des effets de bords ;-) Encore merci ! Edited July 24, 2014 by logs00 (see edit history) Link to comment Share on other sites More sharing options...
Eolia Posted July 24, 2014 Share Posted July 24, 2014 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