If this works correctly, it will redirect to Google if the link was opened from Facebook, or else, it will redirect to your Netlify website (<site-name>.netlify.app/post/1001/).
When I share the above site on facebook, it does not redirect to google.com
But when I share the site below on facebook, it redirects to google.com.But when we click on the domain from a normal place, we see the following error.
Thanks, with your new code, it is redirected to google from facebook, but does not work for facebook mobile traffic.
I think we should also add it as m.facebook.com. We tried to add it ourselves so as not to bother you, but we couldnât find the word ââ facebook ââ in the new code you just provided.
The above code depends on the fact that Facebook adds a query string parameter to the URLs opened from their website. For example, if you share https://www.foo.com/ on Facebook, when clicking on the link, youâll reach the URL: https://www.foo.com/?fbclid=blah. So, the above code is checking if the parameter (fbclid) exists to determine if the URL is coming from Facebook. So, if you manually add ?fbclid=blah to the end of your URL, it will still redirect to Google.
This is the line responsible for the check: event.queryStringParameters.fbclid. About it not working on mobile, Iâll have to check. I didnât really expect the behaviour to be different on phones and computers, but if youâre facing the issue, Iâll try once and let you know.
UPDATE: I just tried opening a link from m.facebook.com and it still linked to the correct thing with the fbclid parameter. So the above code will work just fine.
The problem is on the (Android) app. Facebook opens the link in the in-app browser and doesnât display the URL, so, Iâm not sure if itâs adding the parameter there or not, but Iâd assume it does.
However, if it doesnât work for you, you can get creative and add another parameter at the end of your URLs while posting them on Facebook. So, you can post something like https://www.foo.com/?fb=true. And in the code, you can change event.queryStringParameters.fbclid to event.queryStringParameters.fb. This should do it.
you suggest to apply second process I try to do this but itâs not working. I am using gatsby with Netlify you can check my GitHub repo https://github.com/cuteanimalplanet/cuteanimalplan is everything is okay or I need to do something.
I want to share post in social media and post will redirect to another URL
The method youâre talking about will only work when youâve a query string parameter with the actual URL.
Iâve given an example here:
Can you confirm if thatâs still not working?
Also, from your deploy logs, it seems as if your Functions are not getting deployed at all. Would you try setting the Functions folder in your `netlify.toml file or here: Netlify App?
I see something works but properly not working when I share post on facebook itâs showing main site url in post but I want to show netlify site link in my post see the screenshot for better understand Screenshot by Lightshot
@hrishikesh please note I am using Gatsby with Netlify
I think something working with this code in functions folder into redirect.js file
But Some times itâs not working could you please provide me exact code what I need you can see my github repo https://github.com/cuteanimalplanet/cuteanimalplan
You rejected the solution that youâve posted in your post above because Facebook shows your websiteâs address which you donât want. So, the fact that youâve posted that code in your reply is confusing me.
You havenât correctly copied the code I provided in my previous reply. Itâs event.queryStringParameters.fbclid and not event.queryStringParameters.fb. I had tried this code back when I had posted it and it had worked. So, unless Facebook has changed something, it should still work.
We try our best to help the users but note that providing help with custom code does not fall under the domain of support that we can provide. So, even though Iâm trying to help, Iâm not sure how much in-depth I can further go in this thread. After a point, youâll have to try to manage this yourself.
How it works: It checks for the fbclid query string parameter that Facebook attaches on any external link opened from the platform. HOWEVER, do note that, this might not work well with ad blockers. I am using AdGuard and spent quite some time thinking why it was not working, until I remembered that the fbclid parameter was getting removed by it. In such a case, the function will process the URL as if it didnât come from Facebook.
Now, when the URL comes from Facebook, the user will be redirected to the page on your custom domain (the URL that you enter as an encoded string). However, if the URL doesnât come from Facebook, it will automatically redirect you to the primary domain of the website along with the path from the url query parameter. This has just been tested, so it should work.