Magento: Switch From Grid view To List View, Without Changing URL

Magento: Switch From Grid view To List View, Without Changing URL

October 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Switch From Grid view To List View, Without Changing URL

Switching Magento View Modes Without Changing URLs

As an experienced e-commerce developer, I often receive queries about optimizing the user experience on category pages. One common request is to allow customers to switch between grid and list views without changing the URL of the page they’re on.

In this post, we’ll explore a practical solution for achieving this in Magento 2 using JavaScript/jQuery and some server-side configuration.

The Challenge: Maintaining URLs

Magento’s default behavior changes the URL when you switch views. However, from a user experience perspective, having static, clean URLs is crucial for bookmarking, sharing links, and improving overall navigation.

Solution: Server-Side Configuration

Before we dive into the frontend changes, let’s ensure our server configuration supports this functionality:

  1. Enable Magento 2 Default View Modes By default, Magento allows you to set your preferred view mode (grid or list) at the category level. Ensure this feature is enabled in your admin panel.

  2. Configure Your Server

    For Magento 2 sites hosted on Nginx, ensure your configuration file includes:

    location ~ ^(/index|/pub/)/ {
        try_files $1$2 /index.php?/$1$2;
    }
    

    This rule ensures that any request starting with /index or /pub is forwarded to index.php, allowing for dynamic URL handling.

Frontend Changes: JavaScript/jQuery

Now, let’s enhance the frontend experience using JavaScript (or jQuery if you’re still on Magento 1).

Step 1: Add View Buttons

First, add two buttons to your category view - one for grid and one for list mode. For simplicity, we’ll assume you have a phtml template (catalog_category_view.xml).

<div class="view-mode-buttons">
    <button id="grid-view-button">Grid</button>
    <button id="list-view-button">List</button>
</div>

Step 2: Add JavaScript

Next, add the following script to your page:

<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery("#grid-view-button").click(function(e) {
            e.preventDefault();
            var url = window.location.href;
            if (url.indexOf("mode=list") > 0)
                window.location.href = url.replace(/&?mode=list/g, "mode=grid");
            else
                window.location.href = url + "&mode=grid";
        });

        jQuery("#list-view-button").click(function(e) {
            e.preventDefault();
            var url = window.location.href;
            if (url.indexOf("mode=grid") > 0)
                window.location.href = url.replace(/&?mode=grid/g, "mode=list");
            else
                window.location.href = url + "&mode=list";
        });
    });
</script>

This script captures the click event of each view button. It checks if the current URL contains a mode parameter and replaces or appends it accordingly.

Conclusion

With this approach, you can now provide users with a seamless experience when switching between grid and list views on Magento category pages, without changing their URLs.

Remember to always test thoroughly after making any significant frontend changes. If you’re looking for professional assistance in optimizing your e-commerce platform, feel free to reach out to us at Laravel Company.

Magento 2 Catalog Database Performance

Our network