How to redirect returning visitor?

MGaLL

Junior Member
Joined
Mar 24, 2022
Messages
168
Reaction score
159
Once user click my link he will be redirected to next page after a while.

If user attempt to return back where he started, I need to 301 him to stay redirected.

Once again

User click link visit page A and after a while auto-redirect to page B
If user decides to go back to page A or start over again heading to page A, he must be redirected to page B without being able to see A again.

Thanks
 
It’s not 100% fool proof, but in the page which you want the history disabled, add window.history.forward(1) in a script tag. That should do it.
 
You need to store something in the users local storage so that you know to forward the users that you've already seen.

If you have a SPA type app conditionally render the site based on this logic.

If you are rendering server side then pass the variable to the server and redirect from there.

exampe of local storagw checking and redirect on the front end

JavaScript:
// Function to check if a user has already visited the site
function checkIfUserVisited() {
  // Check if the key 'visited' exists in local storage
  if (localStorage.getItem('visited') === null) {
    // If the key doesn't exist, set the key 'visited' with a value 'true'
    localStorage.setItem('visited', 'true');
  } else {
    // If the key exists, redirect the user to another page
    window.location.href = 'https://www.example.com/other-page';
  }
}

// Call the function when the page loads
window.addEventListener('load', checkIfUserVisited);
 
Back
Top