Skip to main content

Online store customer login redirects

You can configure where customers are redirected after they log in to their account on your store. For example, you might want to redirect customers to the page they were on when they logged in, or a specific product collection.

In this tutorial, you'll learn how to configure custom redirects in your theme.


Use routes object to manage login redirection.


Anchor to Redirecting to customer accountsRedirecting to customer accounts

Before you edit your theme code, review the considerations for customizing your theme to prevent any unintended changes to your store.

In Liquid, the routes object can be used to direct customers to customer accounts, where they can view their order history and track current orders:

  • routes.account_login_url: Takes customers to the login screen and then to the order index page after a successful login
  • routes.account_url: Takes customers to customer accounts order index page directly

If you need to append a path (such as for an account extension, tab, or subpage) to routes.account_url, be aware that this URL may sometimes contain query parameters (for example, after a successful login).

To ensure your generated links remain valid, always split routes.account_url on the ? character before appending your extension, then re-attach any query parameters at the end.

Example:

/sections/header.liquid

{% assign url_parts = routes.account_url | split: '?' %}
{% assign base_url = url_parts | first %}
<a href="{{ base_url }}/pages/extensionId{% if url_parts.size > 1 %}?{{ url_parts.last }}{% endif %}">Account Extension Link</a>

This approach always produces a valid link, regardless of whether routes.account_url contains query parameters.

If your store's theme supports a customer account login link, then the following is an example of the default route on the header.liquid page:

/sections/header.liquid

{%- if shop.customer_accounts_enabled -%}
<a href="{%- if customer -%}{{ routes.account_url }}{%- else -%}{{ routes.account_login_url }}{%- endif -%}"
className="header__icon header__icon--account link focus-inset{% if section.settings.menu != blank %} small-hide{% endif %}"
rel="nofollow">
</a>
{%- endif -%}

If your theme doesn't display a login link in your theme header, then you can add a customer account login link to your theme with the following Liquid code:

/sections/header.liquid

{%- if shop.customer_accounts_enabled -%}
{% if customer %}
<a href="{{ routes.account_url }}">Account</a>
{% else %}
<a href="{{ routes.account_login_url }}">Login</a>
{%- endif %}
{%- endif -%}

Create direct links to redirect customers to specific customer account pages, such as their Profile page:

/sections/header.liquid

{% assign url_parts = routes.account_url | split: '?' %}
{% assign base_url = url_parts | first %}
<a href="{{ base_url }}/profile{% if url_parts.size > 1 %}?{{ url_parts.last }}{% endif %}">Account Profile</a>

Link to a customer account full-page extension with the following Liquid code:

/sections/header.liquid

{% assign url_parts = routes.account_url | split: '?' %}
{% assign base_url = url_parts | first %}
<a href="{{ base_url }}/pages/extensionId{% if url_parts.size > 1 %}?{{ url_parts.last }}{% endif %}">Account Extension</a>

Anchor to Direct customers back to the online storeDirect customers back to the online store

Direct your customers back to the online store after successful login by editing your theme code to use the routes.storefront_login_url object. Once logged in, the customer will be taken back to the page where the login originated. For example, if a customer is on the Catalogs page and clicks on the login link, they will be taken back to Catalogs after a successful login.

The following example demonstrates how you can modify the customer account login link with the storefront_login_url route. In this example, after a customer logs in, they will be taken back to the storefront. If they click on the login link again, they will be taken to customer accounts:

/sections/header.liquid

{%- if shop.customer_accounts_enabled -%}
<a href="{%- if customer -%}{{ routes.account_url }}{%- else -%}{{ routes.storefront_login_url }}{%- endif -%}"
className="header__icon header__icon--account link focus-inset{% if section.settings.menu != blank %} small-hide{% endif %}"
rel="nofollow">
</a>
{%- endif -%}

You can also add the route to any login URL on your storefront with the following Liquid code:

/sections/header.liquid

<a href="{{ routes.storefront_login_url }}">Login</a>
Note

If you choose to direct customers back to the online store after login by replacing the default login link route, we recommend you use another link on your theme that navigates customers to their accounts/orders.


Anchor to Direct customers back to another page on the online storeDirect customers back to another page on the online store

You can redirect customers to any page on your online store using the /customer_authentication/login path with a return_to parameter.

For example, to redirect customers to your Contact page after login, use this Liquid code:

/sections/header.liquid

<a href="/customer_authentication/login?return_to={{ "/pages/contact" | url_encode }}">Contact</a>
Note

The return_to parameter will only work with relative URLs.


Was this page helpful?