Magento 2自动为登录链接添加referer的原因及移除方法咨询
Hey there! Let's break down that weird-looking referer string you're seeing in your login URL and how to get rid of it.
Why the Referer Gets Added
Magento 2 adds this referer parameter by default to boost user experience. Here's the logic: if a user is browsing a product page, cart, or any other section and clicks the login link, the system saves that original page URL (encoded into that long string you see) as the referer. Once the user logs in successfully, they’re automatically sent back to that exact page instead of the account dashboard. It’s a convenience feature to keep users in their original workflow without forcing them to navigate back manually.
The string after referer/ is just a URL-encoded version of the page the user was on before clicking login. If you decode it with a basic URL decoder tool, you’ll see it’s the full, readable URL of that previous page.
How to Remove the Referer from Login Links
You’ve got a few options depending on how permanent or flexible you need the solution to be:
1. Use Magento Admin Configuration (Easiest Way)
If you don’t need the "redirect back to previous page" functionality at all, you can toggle a setting in the admin to eliminate the referer parameter entirely:
- Log into your Magento 2 admin panel
- Navigate to Stores > Configuration > Customers > Customer Configuration > Login Options
- Find the option Redirect Customer to Account Dashboard after Logging in and set it to Yes
- Save the configuration and clear your Magento cache
Once enabled, the system will stop adding the referer parameter to login links, since it knows it should always redirect users to the account dashboard post-login.
2. Custom Module (Permanent, Upgrade-Safe Solution)
If you want to keep the post-login redirect behavior but still remove the referer from the login URL, you’ll need a custom module to override the core URL generation logic. Here’s a quick step-by-step:
- Create a new custom module (if you don’t have one already set up)
- In your module’s
etc/di.xml, add a preference to override the core customer URL model:
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Customer\Model\Url" type="YourVendor\YourModule\Model\Url" /> </config>
- Create the override class
YourVendor\YourModule\Model\Url:
<?php namespace YourVendor\YourModule\Model; use Magento\Customer\Model\Url as OriginalUrl; class Url extends OriginalUrl { /** * Override getLoginUrl to skip adding referer parameter */ public function getLoginUrl($redirectUrl = null) { // Return the base login URL without any referer attached return $this->getUrl('customer/account/login'); } }
- Enable your module, run
bin/magento setup:upgrade, and clear the cache. All login links will now generate without the referer parameter.
3. Template Modification (Quick, Not Upgrade-Safe)
If you need a temporary fix and don’t mind modifying a template (note: this change will be overwritten when you update Magento), you can edit the login link template directly:
- Go to
vendor/magento/module-customer/view/frontend/templates/account/link/authorization.phtml - Find the line that generates the login URL (usually something like
href="<?= $block->getLoginUrl() ?>") - Replace it with
href="<?= $block->getUrl('customer/account/login') ?>" - Clear your cache to see the change
Just remember: this is a band-aid solution—any Magento update will wipe out this edit, so it’s only recommended for quick testing or short-term use.
内容的提问来源于stack exchange,提问作者David Duong

