homepage Welcome to WebmasterWorld Guest from 54.196.225.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
Client side redirects
Any advice welcome, thanks
Stuz




msg:3663147
 7:18 pm on May 30, 2008 (gmt 0)

Ok, I'm going to see if I can explain this properly, lol.

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 :)

 

Trace




msg:3663168
 7:41 pm on May 30, 2008 (gmt 0)

You could do this with a little JavaScript and the referrer property: document.referrer

Simply check the domain (assuming the front page and forum are on the same domain) and if the referrer domain is not equal to current domain, redirect to the front page.

Stuz




msg:3663375
 1:42 am on May 31, 2008 (gmt 0)

That's part of the problem, the front page and the forum are not on the same domain. So I was wondering about how to present the options without creating a neverending loop. But thanks for that tip with referrer, feels like I'm getting closer :)

Stuz




msg:3663377
 2:01 am on May 31, 2008 (gmt 0)

Just wondering, would something like this work?

<script language=javascript>
//
if {document.referrer.indexOf("mysite.com""myforum.com") == -1}
window.location = "http://mysite.com.html";
</script>

Sorry, I'm pretty much a newb here.

penders




msg:3663731
 5:14 pm on May 31, 2008 (gmt 0)

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.

jdMorgan




msg:3663753
 6:01 pm on May 31, 2008 (gmt 0)

The code above appears to cause an infinite redirection loop if the referrer is blank. It will be necessary to check for a blank referrer and bypass the redirect.

It would also be a good idea to NOT redirect if the current location is already the desired redirect location in either case.

Jim

Stuz




msg:3663809
 7:51 pm on May 31, 2008 (gmt 0)

Thankyou very much Penders, that's great and much appreciated.

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]

penders




msg:3663832
 8:29 pm on May 31, 2008 (gmt 0)

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'; }

...is NOT good. This could cause an infinite redirect to the same location; effectively refreshing the page over and over... This checks to see if 'myforum.com' appears at the start of the referrer string (an internal referrer in this case).

NB: In practise the referrer string will probably start 'http://...'

[edited by: penders at 8:37 pm (utc) on May 31, 2008]

Stuz




msg:3663833
 8:36 pm on May 31, 2008 (gmt 0)

Ah, ok thanks. I'm glad you got back to me on that one :)

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.

2lame2rank




msg:3663835
 8:45 pm on May 31, 2008 (gmt 0)

I have to say that this as a whole is a very very bad idea. Someone searches in Google, finds a page which has the information they want, they click on it and are taken away from the page they want to your index page (which they don't want). At best they will not realize there's a redirect and click the Back button because they don't find the info they were looking for, at best they will curse you and click Back never to return to your site at all.

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]

jdMorgan




msg:3663836
 8:47 pm on May 31, 2008 (gmt 0)

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.

Jim

penders




msg:3663840
 8:53 pm on May 31, 2008 (gmt 0)

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>

What this says... If you are not navigating from a page on myforum.com (the referrer does not contain myforum.com) AND you have not navigated from mysite.com (the referrer does not contain mysite.com) then redirect to mysite.com (where a user, who is logged in, should find a link to myforum.com?).

Stuz




msg:3663842
 9:02 pm on May 31, 2008 (gmt 0)

Thanks very much for all replies and thanks for all of your help penders, you've answered all my questions, thanks very much :)

Stuz




msg:3663847
 9:18 pm on May 31, 2008 (gmt 0)

Hiya, sorry, it's me back again. 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. Not sure what I've done wrong there. :(

penders




msg:3663912
 12:00 am on Jun 1, 2008 (gmt 0)

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...

jdMorgan




msg:3663938
 1:07 am on Jun 1, 2008 (gmt 0)

> An undesirable (and persistent) redirect yes

Yes.

Jim

Stuz




msg:3663959
 2:11 am on Jun 1, 2008 (gmt 0)

Just came back to say thanks again to penders, it was my mistake causing it not to work. Now it does exactly what I want it to. Thanks again for all your help and patience, I really do appreciate it. Stuz :)
I have also paid attention to all other comments and thought about them but it was a needed action in the end and it's worked out fine but thankyou for expressing your concerns and opinions.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved