Redirecting referral traffic

Hello, I have been using Netlify for a while, I wanted to try few things and I saw a post not so long ago that some user was needing help similar like mine. The post that I am talking about is:

I tried a few days using this code, if (event.headers.referer.includes(‘website’)), it works perfectly when I am visiting from “website” referral but when I try to visit it directly I have an error that says “includes” is not recognized, is there someone that can help me? I did a lot of re search before posting this and I found nothing. Below is screenshot of the error.

I found that when I did
if(event.headers.referer && event.headers.referer.includes(‘website’))

Solved my problem but then I got another problem

Hey @anncs

Can you provide a link to the function you are deploying? This might assist in tracking the issue.

1 Like

Thanks for your reply, it means a lot to me. Below you can see the repo:

You mentioned this:

but in the function I see

event.headers.referrer && event.headers.referrer.includes("pinterest")

which is different. Note, it is referer and not referrer. This is a misspelling which you can read more about on Wikipedia.

Next

 let pathName = location.pathname.split('/')[2].split('-')

there is no location, in a function, unlike a browser. You can access event.path (e.g. /something) and event.rawUrl (e.g. https://example.com/something). So to get pathName you could use

let pathName = event.path.slice(1)
// returns `something` from the above example
1 Like

You are amazing. I can’t express how much I appreciate your help. I have one last issue. In the else part, which means if the referer is not matching, how I can show them the actual content example from /post/ directory? As you can see on the facebook post below, if you open the link directly (decoded) you’ll notice that it won’t redirect anywhere but instead show the content from that URL. I also opened the the page source and there are not any scripts. I have no idea how he achieves that.

Thank you so much

1 Like

To do that you will need to use the included_files directive in the netlify.toml.

A recent example of this is in the following thread in which I have linked to a demo repository.

coelmay, I have been trying for few days and I thought I could do it by myself but I couldn’t manage it to work with the referral script above, I did made some changes as you can see on my Github but no luck. Thank you so much for your time.

Hey @anncs

The post.js function would fail to build as there is no package.json. If you look at the repository I shared in the post above, there is a package.json file in the repository root. Functions, like a site that uses React, Vue, etc. requires building and without package.json this won’t work properly.

A great place to start is

I did everything same but I couldn’t access the link, you can check it by yourself.

What are the build settings you have used when deploying the repository?

What is the URL you are publishing to?

I tried to access the post with the same URL as you did.
.netlify/functions/post?postId=1

Sorry I don’t understand your first question

Alright now it is working. I have question. How I can make .netlify/functions/post?post-name to redirect to google.com/post-name ? So post-about-keto will redirect to google.com/post-about-keto ? And so on

Check out the Redirects and Rewrites documentation

1 Like

@coelmay, these have all been awesome suggestions and resources! Thanks for sharing! @anncs, let us know if you have further questions. Happy building :rocket: