1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to redirect an html page only once?

Discussion in 'Web Design' started by madiha, Jul 15, 2012.

  1. madiha

    madiha Newbie

    Joined:
    Mar 4, 2012
    Messages:
    33
    Likes Received:
    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: Jul 15, 2012
  2. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    500
    Likes Received:
    257
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
    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 Thanks x 1
  3. madiha

    madiha Newbie

    Joined:
    Mar 4, 2012
    Messages:
    33
    Likes Received:
    2
    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?
     
  4. madiha

    madiha Newbie

    Joined:
    Mar 4, 2012
    Messages:
    33
    Likes Received:
    2
    Anyone?
     
  5. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    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"); ?>
    
     
    • Thanks Thanks x 1
  6. madiha

    madiha Newbie

    Joined:
    Mar 4, 2012
    Messages:
    33
    Likes Received:
    2
    This would have been it but I don't have access to page2.php Can I still make it work?
     
  7. madiha

    madiha Newbie

    Joined:
    Mar 4, 2012
    Messages:
    33
    Likes Received:
    2
    Will it work if page2.php is hosted on a different server?
     
  8. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    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 Thanks x 1
  9. madiha

    madiha Newbie

    Joined:
    Mar 4, 2012
    Messages:
    33
    Likes Received:
    2
    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?