In this quick tip we will see how to deal with Prestashop redirections in the cleanest way. In this example, we will show you how to redirect your clients after they have logged in.
- Tested Version: Prestashop 1.6 (Should also work well with 1.5).
Redirecting in Prestashop
We do not recommend using the normal redirections with PHP headers. Instead, Prestashop has its own methods to redirect users to the page you want, specifically for the front and back office.
Redirecting users in the Front office after login
We can use two methods to manage Prestashop Redirections in the front office:
The first one is what I generally use:
redirectLink can be used the same way. However, the previous one also allows us to use a custom base URL, a link class and custom headers:
Tools::redirect($url, $base_uri = __PS_BASE_URI__, Link $link = null, $headers = null)
They are generally not needed, but might come in handy for specific cases. If you only use the page name, like I used order above, make sure it exists within the shop. Therefore, something like the following will not work (unless you really setup a specific .htaccess rule for it)
And you will end up with a 404 page
Knowing that, let’s see how to redirect our users after logging in. We will not use overrides to be quicker.
Open up AuthController.php located in controllers/front. Locate the following, around line 620:
if (($back = Tools::getValue(‘back’)) && $back == Tools::secureReferrer($back))
// redirection: if cart is not empty : redirection to the cart
if (!$this->ajax && $back = Tools::getValue(‘back’))
if ($back == Tools::secureReferrer(Tools::getValue(‘back’)))
$back = $back ? $back : ‘my-account’;
Tools::redirect(‘index.php?controller=’.(($this->authRedirection !== false) ? urlencode($this->authRedirection) : $back));
We can get rid of most of it as we really want users to reach our custom page (in this case, a product)
This, will, no matter what, send users to that product’s page when they login
Prestashop redirection in the Back Office
To redirect to a page of the admin interface, it will be enough to use the following
However, retrieving the URL might be a bit troublesome since Prestashop uses a token to secure back office pages. In this case, we first need to grab the URL as follows:
$link = $this->context->link->getAdminLink(‘AdminProducts’);
$link will contain the needed token so that your back office page won’t crash to an “invalid token” error.
Always retrieving correct links
Here is a list of methods you can use to retrieve various entities’ links in Prestashop (please make sure you use the correct context):
// page link, like ‘index’, ‘order’…
// CMS page link, first parameter being the cms id
// Various entities, with id as the parameter
// Modules, with parameters like (‘modulename’, ‘controller’, array(1,2,3)), the array being a list of querystring parameters
$this->context->link->getModuleLink(‘psticket’, ‘mytickets’, array(‘id_ticket’ => 1, ‘example’ => 1))