Forum Moderators: open
I have a website that links to a free hosted forum. You can't see the link on the website unless you log in. But what I want to do is this, if somebody goes straight to the forum (it's a phpBB2) I want to be able to redirect them to the website. But if they come from the website then they should be allowed access. Also, if they are already on the forum and go back to the forum index, I don't want this to kick them back to the website either. The problem is, on the forum I don't have server access or any other so any script or code would have to be done on the forum index (the main forum webpage that is), in html. The index will accept html in a forum description so if I made a forum title and description, I could enter the script in there. I have danced around the answer whilst searching but everything always seems to come back to it being a permenant redirect and this is not what I'm looking for. So to sum up...If somebody goes from the main website to the forum that is fine, if they are on the forum and go to the forum index (it's front page) this is fine, but if they come from anywhere else I want to redirect them to the main website. Is this possible to do? Hopefully somebody can give me some advice and thanks for reading through all of that :)
That's part of the problem, the front page and the forum are not on the same domain.
I think Trace was refering to the forum index, rather than the front page of your website? Unless there are 3 sites involved?!
The referrer property holds the URL the user came from in order to get to your website. So if a user clicks a link on your website to get to the forum site then if you check the referrer on the forum site it should contain the url of your main website. If it doesn't then you want to redirect them back to your main website.
The referrer is sent by the browser. It is empty if the user types the URL into their browser (or follows a bookmark?). Just be aware that it is not 100% accurate and can be faked. Browser plugins for FF enable you to set any referrer you like! But anyway...
The index will accept html in a forum description so if I made a forum title and description, I could enter the script in there.
Does the forum even allow you to enter script?
Your script is along the right lines, just watch your curly brackets and indexOf() method parameters...
<script type="text/javascript">
if (document.referrer.indexOf('mysite.com') == -1) {
location.href = 'http://mysite.com/index.html';
}
</script>
The .indexOf() object method searches for 'mysite.com' within the document.referrer string. If it is not found it returns -1 (otherwise it returns the position in the string >=0)
This will never cause a 'neverending loop'. The worst that can happen is that the browser does not send the correct referrer so they never get to your forum! Also bear in mind that with JS disabled, your check is bypassed, and they will be able to get to the forum without going via your website.
Thanks also jd, I'll try to do that.
Thankyou both for your time and thanks Trace for the original lead.
And I'm sorry to be a pain and continue this but I thought I'd better check before I make a hash of things. So the whole thing should look something like this.....
<script type="text/javascript">
if (document.referrer.indexOf('myforum.com) == 0) { location.href = 'myforum.com/index.html'; }
else if (document.referrer.indexOf('mysite.com') == -1) { location.href = 'http://mysite.com/index.html'; } </script>
Again, sorry to still be going on :(
[edited by: Stuz at 8:11 pm (utc) on May 31, 2008]
The code above appears to cause an infinite redirection loop if the referrer is blank.
Actually (unless I have misunderstood the original query) the code snippet in my post above should go in your myforum.com site. You only want to redirect from myforum.com to mysite.com - this should not (cannot) cause an infinite redirection loop. You are never 'redirecting' (or refreshing) back to the same location.
A blank referrer (ie. the user has probably gone directly to your myforum.com site) would cause a redirection to mysite.com. mysite.com should not contain any redirection code.
However, this line:
if (document.referrer.indexOf('myforum.com) == 0) { location.href = 'myforum.com/index.html'; } NB: In practise the referrer string will probably start 'http://...'
[edited by: penders at 8:37 pm (utc) on May 31, 2008]
Just one last question though, if somebody was in one of the forums or on a thread and then went back to the main forum index would this then redirect them back to my main site as they hadn't come from there to get to the index? This is something I want to avoid.
Think VERY carefully about whether you want to do this.
Anyway, doing this client-side is fraught with difficulties. What about blank referrers? What about people saving forum pages in their bookmarks and can't get back to the page because of a silly redirect?
You say you are looking for advice, so please consider dropping this idea. Remember, every page on your site is a welcome page, so try to always be welcoming and not push your potential clients about.
Oh, and finally, Google frowns upon "sneaky redirects" (their words), so you could even get your site banned in Google for this.
[edited by: 2lame2rank at 8:47 pm (utc) on May 31, 2008]
if somebody was in one of the forums or on a thread and then went back to the main forum index would this then redirect them back to my main site as they hadn't come from there to get to the index?
Ah yes! (I see now what you were trying to do above :)
To resolve this I think you just need to avoid doing anything if the referrer is the site itself (myforum.com)... Code on myforum.com:
<script type="text/javascript">
if ((document.referrer.indexOf('myforum.com') == -1) && (document.referrer.indexOf('mysite.com') == -1)) {
location.href = 'http://mysite.com/index.html';
}
</script>
That bit of script just caused a loop that always took me back to my main site, even if I came from the main site.
Do you really mean a 'loop'? Or a one-time redirect? I've just tested this out and it works OK for me in IE6, FF1.5 and Opera8...
[1] You can only access myforum.com (main page) from a link on mysite.com (you don't get redirected back to mysite.com)
[2] If you try to access myforum.com (main page) directly or from another site you get redirected to mysite.com, where there is a link to myforum.com (main page) - see [1]
[3] If you access myforum.com (main page) from another page on myforum.com then you don't get redirected back to mysite.com
jdMorgan: If the referrer is blank, you must "do nothing" in order to avoid loops -- with or without the user involved. If it's blank, you basically just cannot make a redirect/no-redirect decision.
I fail to see how any 'loop' could result? An undesirable (and persistent) redirect yes, but no loop. If the referrer is blank it is quite probable that the 'average' user has typed the URL directly and they should be redirected. The referrer is also blank (in IE) after a JS redirect, after having been redirected back to mysite.com, but there should be no code on mysite.com that checks the referrer - OK. However, the referrer could also be blank for other reasons... security/firewalls and browsers simply not sending the referrer header - unfortunately these users would never be able to get to myforum.com (main page). I assume that Stuz has deemed this acceptable for his particular site/user base?
2lame2rank: I have to say that this as a whole is a very very bad idea.
For your average site on the www where you are trying to attract visitors; yes, this is a bad idea. As you say, every page is a 'welcome page'. However, it may be acceptable for this particular site and user base? (Perhaps it's a local intranet on a closed set of machines/browsers? Stuz does talk about having to be 'logged in'.) To satisfy this requirement under the constraints outlined above is never going to be robust, but a simple client-side redirect might be acceptable in this case? May be...