Magento 1.x Redirect Login Form After Login

The problem is simple:

  • After a user logs into the website, redirect them to a specific URL.

The solutions across the internet are vast and code heavy.  But this solution really only requires a simple change to your template file.

In the login template:

persistent/customer/form/login.phtml

Update the form action to include your specific redirect path.

This is what exists currently:

What I suggest is writing your redirect into a PHP variable, and then apply that to the action parameter.  Like this:

Let me explain what is happening here a bit.

  • The core Magento code expects the $session->setBeforeAuthUrl to be set to respect the referer query parameter.  For that reason, I simply set it to the homepage.  This is really just a failsafe in-case something goes wrong.
  • The $redirectUrl is the URL you’d like your visitors to land on after logging in.
  • The $formActionUrl simply takes the current form post URL that exists and adds the referer query parameter with your redirect URL.  The one bit of “trickery” here is to pass it through Magento’s urlEncode function in the core helper which base64 encodes the URL.
    • If you’re asking what does base64 encode mean/do???, don’t worry it’s nothing big you need to worry about.  But simply put, it turns the url into garbly-gook – which is how Magento expects the URL to be formatted.  Later, Magento will un-garbly-gook (yes, a technical term) the URL on the backend so they know what it means.

 

And that’s it – simple as that.  No modules, fancy trickery, etc.  But of course remember to copy this template to your theme folder (don’t edit in the base or default directory) before making these changes.

I hope that helps someone!