Redirect to URL in a URL field

Hello, I am looking to redirect any record using it’s unqiue Wix ID to a URL saved in the (URL) field called ‘URL’

example:

https://www.abc.com/redirect/127c37f1-d5e9-4b0d-b27f-750dbf77aa55

would redirect to:

https://www.xyz.com/12345

I have gotten the basic static code below to work:

import wixLocation from 'wix-location';

$w.onReady(function () {
     wixLocation.to("https://www.xyz.com");
}) 

So dynamic code would need to be something like:

import wixLocation from 'wix-location';

$w.onReady(function () {
     wixLocation.to('URL');
}) 

Hi there :wave:t2: I do something similar on my site, but just want to clarify what you’re trying to accomplish. What’s your setup like? Is this on a dynamic page, or a repeater? Does this happen when a user presses a button?

I have a digital boating magazine on Issuu.com (converted PDF to their format). Each boat is also in a searchable database/collection on my Wix site. There’s over 300 boats, so I automate the layout of the PDF file (like a mail merge) and place a URL under each boat photo in the format of https://www.mysite.com/boat/127c37f1-d5e9-4b0d-b27f-750dbf77aa55 .

Currently, when a user clicks the link on Issuu.com page it goes to a boat detail page on my site with more photos, etc. I would like to change it so that when a user clicks the link it goes to a redirect page like https://www.mysite.com/redirect/127c37f1-d5e9-4b0d-b27f-750dbf77aa55 and then redirects the user to the URL saved in a URL field for that boat record, which is the local boat dealer’s detail page with photos, etc., like https://www.b oatdealer.com/24-sea-ray-2011

So they land on a dynamic page, and you want to redirect them to another link in a database field called url? Is that right?
I haven’t tested this, but I’m guessing you could use getCurrentItem and then just send them to the url field. Something like the code below.

$w("#yourDataset").onReady(() => { 
    let currentItem = $w("#yourDataset").getCurrentItem();
    let link = currentItem.url;
    wixLocation.to(link);
})

Good luck!

Yes, they land on dynamic page. I used the below code and got this error. The BoatData (ID) is referencing Collection name and Field name of each Wix ID for each record.

ReferenceError: wixLocation is not defined
(Line 4)
BoatData (ID)

$w("#dataset1").onReady(() => { 
    let currentItem = $w("#dataset1").getCurrentItem();
    let link = currentItem.url;
    wixLocation.to(url);
})

OK, I realized my code didn’t match your sample code. So I updated to below and still error message during preview. But live the page loads but does not redirect. I put the text of the URL field and ID field on the page and they both show on the page.

$w("#dataset1").onReady(() => { 
    let currentItem = $w("#dataset1").getCurrentItem();
    let link = currentItem.url;
    wixLocation.to(link);
})

Do I need this on line #1?

import wixLocation from 'wix-location';

Yes you do need to import wixLocation, and the final code should be within the onReady for the page. Let me know if this works, or we may have to find another method.

import wixLocation from'wix-location';

$w.onReady(function() {
	$w("#dataset1").onReady(() => {
		let currentItem = $w("#dataset1").getCurrentItem();
		let link = currentItem.url;
		wixLocation.to(url); 
	});
})

This works! Thanks so much for the guidance. Much appreciated. Is there any technique to not have the Wix page show or load during the redirect? I think when I used to use old HTML redirects like <metahttp-equiv="refresh"content=“time; URL=new_url” /> the page would not show, but just redirect to new page.

The easiest way would probably be to set the page elements (strips, etc) to be collapsed or hidden on load in the properties and events panel . Though I must ask, if you are always hiding this page why are there things on it at all?

If you intend to use this page in other circumstances, then the code above is not desirable as it will always redirect. If you only want to redirect if users are coming from a specific destination (your issue.com site), then you should try using wix-window/referrer . This forum post has a good example too.