You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magento 2自动为登录链接添加referer的原因及移除方法咨询

Why Magento 2 Adds a Referer to Login URLs & How to Remove It

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.

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:

  1. Create a new custom module (if you don’t have one already set up)
  2. 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>
  1. 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');
    }
}
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:17:16