How to redirect an html page only once?

madiha

Newbie
Joined
Mar 4, 2012
Messages
33
Reaction score
2
I want to redirect an html page on my website (lets say, 1[SUP]st[/SUP] page) to another html page on a different website (lets say, 2[SUP]nd[/SUP] page) and then again from this 2[SUP]nd[/SUP] page back to the 1[SUP]st[/SUP] page on my website. i.e.
Redirect this page:
Code:
mysite.com/page1.html
to:
Code:
othersite.com/somepage.html
(referred to as 2[SUP]nd[/SUP] page from here on)

and finally back to:
Code:
mysite.com/page1.html
(referred to as 1[SUP]st[/SUP] page from here on)

I want no more redirects from this point onwards...

I am currently using this code on the 1[SUP]st[/SUP] page (my website):
Code:
<meta http-equiv="REFRESH" content="5;url=http://www.othersite.com/somepage.html">
As you can see that this code redirects the visitors to the 2[SUP]nd[/SUP] page which will then redirect them again back to the 1[SUP]st[/SUP] page.

But the problem with it is that, when the visitors return back to the 1[SUP]st[/SUP] page, they are again redirected back to the page they came from (2[SUP]nd[/SUP] page). Thus, the users end up in an endless loop.

What I would like done is that, when the first time a user gets redirected to the 2[SUP]nd[/SUP] page (from the 1[SUP]st[/SUP] page) and then again back to the 1[SUP]st[/SUP] page (from the 2[SUP]nd[/SUP] page) s/he should not be redirected back again to the 2[SUP]nd[/SUP] page and stay on the same 1[SUP]st[/SUP] page. i.e. the 1[SUP]st[/SUP] page's code redirects the visitors only once when they visit it for the first time to the 2[SUP]nd[/SUP] page, the 2[SUP]nd[/SUP] page then redirects them back to the 1[SUP]st[/SUP] page but here the 1[SUP]st[/SUP] pages' code doesn't redirect them again to the 2[SUP]nd[/SUP] page and so the visitors end up on the 1[SUP]st[/SUP] page.
How can I modify the code above so that it does what I want it to do?
 
Last edited:
You'll need PHP for this.

Check the 1st page for $_SERVER['HTTP_REFERER'] == 'othersite.com/somepage.html'
If its true, then do not redirect.

That should be all ;-).
 
thanks mate .. but I couldn't make anything out of it ... Sorry .. but I am a noob at web designing .. So can you please show how to do it step by step or point me to a tutorial that does so...

and will the php redirect work on .html pages?
 
page1.php:
Code:
<?php if (!$_SERVER['HTTP_REFERER']) header("Location: http://www.example.com/page2.php"); ?>

page2.php
Code:
<?php header("Location: http://www.example.com/page1.php"); ?>
 
page1.php:
Code:
<?php if (!$_SERVER['HTTP_REFERER']) header("Location: http://www.example.com/page2.php"); ?>

page2.php
Code:
<?php header("Location: http://www.example.com/page1.php"); ?>

This would have been it but I don't have access to page2.php Can I still make it work?
 
page1.php:
Code:
<?php if (!$_SERVER['HTTP_REFERER']) header("Location: http://www.example.com/page2.php"); ?>

page2.php
Code:
<?php header("Location: http://www.example.com/page1.php"); ?>

Will it work if page2.php is hosted on a different server?
 
If you have no control over what page2.php has, or does, then no. If it was possible, I'd be using google.com for affiliate marketing.

If its on a different servers, that's fine. You just need to put redirect code on it.
 
If you have no control over what page2.php has, or does, then no. If it was possible, I'd be using google.com for affiliate marketing.

If its on a different servers, that's fine. You just need to put redirect code on it.

Thanks for replying mate...

It did work even though I have no control over the page2.php .. but it only works if url is copy pasted in to the address bar but doesnt work when clicked from other website like myblog.blogspot.com or reddit.. is there any fix for this?
 
Back
Top