Restricting access to an entire subdomain Answered
Hello everyone,
First off, a big thank you to the Memberstack team for creating such a fantastic product! 🙏
I'm reaching out to ask if anyone knows how to restrict access to an entire subdomain?
While I've successfully implemented Memberstack on both my main domain and subdomain (and managed to restrict certain elements on both websites), my primary goal is to block non-members from accessing the subdomain entirely and redirecting them to the login page on the main domain.
I guess I might be entering wrong the info in the gated content area... Or is this impossible with basic features? Anyone can help?
Comments
11 comments
Hi Antoine,
If you're using the same App ID for the main domain and sub domain, then technically, non-members will not gain access to the either of them if you've gated them.
The only thing I'll suggest is since you want non-members to be redirected to the main domain, you may have to create an extra page for the access denied URL section of the gated content on Memberstack, you put the link to the page.
Then on the page, add the following meta tag before the closing section.
With this, when a non-member visits the subdomain, they get redirected to this page which instantly takes them to the main domain.
P.S: You may have to create the same page in the main domain too as it shares the same access denied URL. Also, remember to replace the https://www.urltofthemaindomain.com in the code above with the main domain URL.
Hi Chukwudi, ok I'll try the redirect, thanks for the code. But my main issue lies upstream. Yes I have same app Id on both domains, but how to gate the entire sundomain ? In gated content admin page I have gated Url start with or equals app.mywebsite.com but this is not working. Not sure we can gate a subdomain there or that this is the right way to do it.
If you've added the subdomain to the application domains in Memberstack, It should work for you. However, if you're still having problems with that even after adding it, please let me know.
Antoine Charrier can you use gated content to gate access to all pages on the domain?
Chukwudi Onyekwere I c an only add 1 domain there right ? I have mywebsite.com
I can gate elements on the subdomain, but how to gate the full subdomain ?
here we need the possibility of adding a sub-domain
In the Memberstack section in your screenshot, you can only gate pages/directories/page elements of a domain/subdomain, not the entire domain/subdomain as you've stated.
However, if you want to achieve that, you may have to use a Javascript workaround instead. The code snippet below when added in the head code section of the site settings of your subdomain, redirects anyone who isn't logged in (i.e non-members), to the URL you specified in URLToTheOtherDomainGoesHere
Here's the code snippet:
Do not forget to replace URLToTheOtherDomainGoesHere with the Redirect URL.
You may also want to enable cross-domain login too. Quick demo video here.
Thank you! My cross domain login is working. But the redirect still not. I'm doing in vanila js. Would you mind sharing a vanila version of this code if possible please ?
The code is Vanilla JS actually 😌.
You can put the code before the closing </body> tags of all the pages in your subdomain and it will work.
Alternatively, you can put the code snippet (without the opening and closing script tags) in a separate file in your project, then name the file something like myscript.js while in the pages on your subdomain (before the closing </body> tags), you can reference the file thus:
I found a random article that wil help in setting this up here just in case.
Thanks!
(Ah ok I tough since "DOM" was inside the code it means the package memberstack must be installed in my server.)
I tried the first method (to place the code before </body> tag instead of before </head> tag but it still does not work :
(index):86 Uncaught TypeError: Cannot read properties of undefined (reading 'getCurrentMember') at (index):86:32
I tried also calling the seperate file as you explained (thanks for the tuto) but does not work neither.
can look at my scripts before </head> please?
please not that cross domain login is working. Only the redirect is not
Your insight is valuable, and we'll definitely take it into consideration. Kindly DM me your app id and website URL so I can look into the errors you're getting.
Thanks ! 🙂
Please sign in to leave a comment.