How can I successfully redirect my old PHP URLs to new Shopify URLs?

How can I successfully redirect my old PHP URLs to new Shopify URLs?

preetcomm
Shopify Partner
8 0 0

Hi,

 

I have moved my website from php to shopify. Earlier my php website ranked well but the url's are different with existing one.  I have redirected 40% of my Url's but rest was unsuccessful. I don't know is there any option for this or not. 

 

My previous url  is /category.php?id=26/smart-shoes

my new url is /collections/smart-shoes

 

My problem is that shopify not accepted my previous url type for redirection. it automatically converts my previous url path into this "/category.php?id=26%2Fsmart-shoes" which is incorrect. 

 

kindly share the solutions.

Replies 3 (3)

okur90
Shopify Partner
126 20 19

Hi @preetcomm 

 

Unfortunately, Shopify doesn't support query parameters (? and &) in redirects, which is the reason you're facing this problem.

 

However, there is a workaround to handle this situation using JavaScript on your 404 error page.

 

First, create a new page template in your Shopify theme, for example, "page.404.liquid". Copy the content of the "page.liquid" template to the new "page.404.liquid" template.

 

Next, add the following JavaScript code at the end of the "page.404.liquid" file, before the closing `</body>` tag:

 

<script>
  document.addEventListener('DOMContentLoaded', function() {
    var redirectToMap = {
      '/category.php?id=26/smart-shoes': '/collections/smart-shoes',
      // Add more redirects as needed
    };
    
    var pathname = window.location.pathname + window.location.search;
    var redirectTo = redirectToMap[pathname];
    
    if (redirectTo) {
      window.location.href = redirectTo;
    }
  });
</script>

 

Replace the example URL in the `redirectToMap` object with your specific URLs. You can add more redirects by adding new lines with the format `'/old-url': '/new-url',`

 

Create a new page in your Shopify admin with the title "404" and assign it to the "page.404" template. You can leave the content of the page empty.

 

Go to Online Store > Navigation > URL Redirects, and remove any unsuccessful redirects that you have created earlier.

 

Finally, go to Online Store > Preferences > scroll down to the "404 error page" section, and select the "404" page you created earlier.

 

Now, when someone visits an old URL that isn't redirected, they will be redirected to the corresponding new URL based on the JavaScript code added to the 404 error page.

Code Slingin, Pixel Wranglin - Laugh it up at onlinex.com.au
preetcomm
Shopify Partner
8 0 0

Thanks

 

I will test it 

p1Commerce
Shopify Partner
129 17 67

This is a late reply but wanted to post up if anybody else comes across this.

Redirects with javascript will create a 302 response which indicates to Google that it's a temporary move. You want to use a 301 redirect to the largest extent possible.

%2F is the url encoded version of a slash.

If you create a redirect for /category.php?id=26%2Fsmart-shoes then it will redirect when /category.php?id=26/smart-shoes is input in the browser using a 301 redirect.

Appreciate the assistance? Please hit the Like and Accepted Solution buttons.
--
An Exceptionally Technical eCommerce Firm
Entirely Focused on Search Position - p1commerce.com