How to Redirect or Delete Products Tag URLs?

Hi everyone,

We have 3 collections, each containing 10 products. Previously, we used tags in products, which resulted in the creation of unnecessary URLs pointing to the same products. For example:

  • Original Collection URL: /collection/collectionname
  • Tag Filter URLs: /collection/collectionname/tag-1, /collection/collectionname/tag-2, etc.

I’ve removed the tags from the products, but these URLs are still accessible with a 200 status code. I want to redirect them to the original collection page, but redirection isn’t working because it only triggers when the URL returns a 404 error.

Can anyone advise how to delete or redirect these URLs effectively?

@rehman75850 Hey, thanks for posting here.

Open your theme.liquid or collection.liquid template.

Add this JavaScript snippet at the bottom, just before </body>:

<script>
  // Redirect if URL contains a tag filter
  if (window.location.pathname.match(/\/collections\/collectionname\/.+/)) {
    window.location.href = '/collections/collectionname';
  }
</script>

This works client-side and will redirect users automatically.

This will be for all tags’ filter URLs? For the selected collection in script?
e.g; /collection/collectionname/tag-1, /collection/collectionname/tag-2, /collection/collectionname/tag-3, redirects to /collection/collectionname

And before using this script code, should we keep the redirect request in redirects panel or should remove it?

You can control, add, and edit any url re-direct within your store from the Menus Tab in Admin.

I’d try this code:

{% if request.page_type == "collection" and  current_tags %}
  <meta http-equiv="refresh" content="0; url={{ collection.url }}" />
  <script>
    window.location.path = {{ collection.url }}
  </script>
{% endif %}

If your theme code is not edited, you can put it into “Custom liquid” section in a “Footer group” or as part of Collection template.

If your theme code is already edited, you may edit layouts/theme.liquid and put the code inside the <head> for faster redirects…

Hi @rehman75850,

You can also create redirects from the Shopify dashboard. Going to Content → Menus → URL redirects

Hi @rehman75850,

Please go to Actions > Edit code > layout > theme.liquid file, find ‘‘ and add code here:

Code:

{% if request.page_type == 'collection' and current_tags %}
      <script>
        window.location.href = '{{ collection.url }}';
      </script>
    {% endif %}

I added this script to redirect our specific collection’s tags URLs to specific collection main URL:

{% assign redirect_collections = 
        "/collections/zaria-ii-unstitched/3pc-pert,
        /collections/zaria-i/zaria-ii,
        /collections/zaria-i/zaria,
        /collections/zaria-i/unstitched,
        /collections/zaria-i/new-arrivals,
        /collections/zaria-i/3pc-pert,
        /collections/zaria-ii-unstitched/zaria-ii,
        /collections/zaria-ii-unstitched/unstitched,
        /collections/zaria-ii-unstitched/stitched,
        /collections/zaria-ii-unstitched/new-arrivals,
        /collections/zaria-ii-unstitched/3pc-pert" | split: "," %}

      {% if redirect_collections contains request.path %}
        <!-- Redirect from specified URLs to /collections/lawn-suits -->
        <meta http-equiv="refresh" content="0; url=/collections/lawn-suits" />
        <script>
          window.location.href = "/collections/lawn-suits";
        </script>
      {% elsif request.page_type == "collection" and current_tags %}
        <!-- Redirect the collection page to its own URL if tags are present -->
        <meta http-equiv="refresh" content="0; url={{ collection.url }}" />
        <script>
          console.log('test', collection.url)
          window.location.path = {{ collection.url }}
        </script>
      {% endif %}

But this code is processing redirections like:
From “/collections/zaria-ii-unstitched/3pc-pert” to “/collections/zaria-ii-unstitched”
But I wrote the script to redirect “/collections/zaria-ii-unstitched/3pc-pert” to “/collections/lawn-suits”.

How can I make it happen?

I also have removed the script so these all requests can be removed. But the redirects are still happening, how can we resolve this issue?

I am happy to delete these tags based URLs to make them 404, so it can be easy to make a redirection request from menu of shopify management. But How can I delete these tags based URLs?

What you’re observing means that only {% elsif %} part is firing.
It means that first condition does not match.

And this happens because of the way you’re creating your redirect_collections array – you’re splitting your list by comma, but there also is a newline character(s) and spaces at the beginning of each line.

To confirm this, add something like:

{{ redirect_collections | json }}

You need to keep this string in one line and avoid extra spaces.