I want to redirect product recommendation string query URLs to non-string query URLs in Shopify and also want that they should return 301 status code while redirecting.
For example from this URL:
https://ese.net/products/some-product?pr_prod_strat=collection_fallback&pr_rec_id=503f88472&pr_rec_pid=8474186613075&pr_ref_pid=8461060833619&pr_seq=uniform
To this:
https://ese.net/products/some-product
I’ve tried to add redirect in 301 redirection manager of Shopify but the redirection not worked for string query URLs.
redirect setup in shopify but not worked
Then I’ve added the JavaScript code in theme.liquid file to redirect string query URLs to non-string query URLs. It worked but its not returning 301 status code while redirecting. I want that when it redirect via JavaScript it should return 301 status code or also share if there is any other method to redirect them.
<script>
document.addEventListener('DOMContentLoaded', function() {
var url = new URL(window.location.href);
var params = new URLSearchParams(url.search);
if (params.has('pr_prod_strat')) {
// Remove the query string from the URL
url.search = '';
// Redirect to the URL without query string
window.location.replace(url.toString());
}
});
</script>