How to add URL exclusion logic to Memberstack redirect script to skip signup and homepage? Answered
Hey Everyone – I am building some custom redirect workflows based on the MS [Code] tutorial on redirecting users to the same page they currently logged in or signed up on.
Works great, but it fires on every page on the website. So, my question is how can I customise this to EXCLUDE a list of URLs? These URLs are pages like ‘the sign up page’, ‘the homepage’. Because that doesn’t make sense to get redirected to a sign up page if you’ve just signend up.
Anyone wannna check my code? Have got somethign working with ChatGPT but always nice for a human to check over
Comments
3 comments
Hey Alex Siale!
I simplified your code a little bit.
Because you are redirecting the user to the same place (the dashboard), you don't need to include it in your excludedURLs array. Instead, you can just have it directly in your if statement. I also removed https://townhouses.nz from your excluded URLs array, as it is redundant. Visitors will always be redirected to the www subdomain, so it won't be possible for them to ever be on the root domain.
<script> MemberStack.onReady.then(function(member) { // Check if the user is already logged in if (!member.loggedIn) { // Listen for the member.login event to trigger the redirect after login member.on('login', function() { runRedirectScript(); }); // Listen for the member.signup event to trigger the redirect after signup member.on('signup', function() { runRedirectScript(); }); } }); function runRedirectScript() { // Define an array of excluded URLs const excludedURLs = [ 'https://www.townhouses.nz', 'https://www.townhouses.nz/new-user-signup', 'https://www.townhouses.nz/signup' ]; // Check if the current URL is in the excluded URLs array const currentURL = location.href; const isExcluded = excludedURLs.some(url => currentURL.includes(url)); if (!isExcluded) { // Save the URL of the page you're on for redirecting back to it localStorage.setItem('memberstack-location', currentURL); } else { // Redirect to the account dashboard window.location.href = '/account/dashboard'; } } </script>Hey Alex Siale!
Check this out, released on Friday 😊
https://webflow.com/made-in-webflow/website/redirect-to-attempted-page
It's similar to the one you're using, but it has an exclude list (and it also works when people are attempting to access gated content and then redirected)
Wow!
You made the first version I was using.
Julian Galluzzo you’re a genius!! thanks mate
success page code:
Please sign in to leave a comment.