I’m trying to extract query parameters from a URL and use them to redirect to a dynamic page using wixLocation.to(). I cannot get it to redirect. The URL I am presenting to the page is www.powercore.nz/vectary?Title=Powercore1400&Size=Fourteen . I want it to redirect to powercore.nz/vectary/Powercore1400/Fourteen . Below is my code. Can anyone guide me where I have gone wrong ?
import wixLocation from ‘wix-location’;
$w.onReady(function () {
// Log the entire query object
console.log(wixLocation.query);
// Get the values of the parameters from the URL
const param1Value = wixLocation.query.Title;
const param2Value = wixLocation.query.Size;
import wixLocationFrontend from 'wix-location-frontend';
$w.onReady(function () {
let query = wixLocationFrontend.query;
if (query) {
let title = query.Title;
let size = query.Size;
let url = wixLocationFrontend.baseUrl + "/vectary/" + title + "/" + size;
wixLocationFrontend.to(url);
}
});
Thanks so much for this Pratham - really appreciate your time. So the code works and it redirects to the new URL but when you set the page settings:
Then it doesn’t work
I added some console.log() into the different stages to see where it was going wrong and it seems that if you set the page settings to receive the correct query parameters then it doesn’t seem to run the code at all ? If you leave the page settings at simply “vectary/” it will redirect as per your code but it won’t return anything ??
Don’t worry i worked it out. I realised that you had to run the code on another page first because the dynamic page won’t exist until you generate it using the parameters. I ran it on another page and then redirected using the parameters to the dynamic page. Thank you SOOO much for your help