How do I create redirect script in PHP (not using htaccess)

Discussion in 'PHP' started by CleverDick, Jul 28, 2009.

  1. #1
    Hi,

    I have two domains pointing to one hosting account. Let's call them:

    domainA.com
    domainB.com

    domainB.com is parked on domainA.com. So if you go to either domain, the same content is displayed. However I want it set so if you go to domainB.com you are redirected to domainB.com/mydirectory

    I have tried this using .htaccess and it doesn't work. It might be interfering with the content management system on the site. So I was hoping to achieve the same result using PHP. I found some code that almost works, but it creates a loop:

    <?php

    $domain = $_SERVER["HTTP_HOST"];


    if (($domain == "domainB.com") ||
    ($domain == "www.domainB.com")) {

    header("location: /mydirectory");

    }
    ?>

    Can I add something to this script that effectively says "if someone enters the site using the domainB.com then redirect them to domainB.com/mydirectory unless this has already happened"?
     
    CleverDick, Jul 28, 2009 IP
  2. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    May be this will help you


    <?php

    $domain = $_SERVER["HTTP_HOST"];
    $fullpath=explode("/",$_SERVER["REQUEST_URI"]);

    if (
    (($domain == "domainB.com") ||
    ($domain == "www.domainB.com") ) && !in_array('mydirectory',$filepath)) {

    header("location: /mydirectory");

    }
    ?>
     
    vetrivel, Jul 28, 2009 IP
  3. CleverDick

    CleverDick Member

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Yes - except that you wrote "$filepath" in the second last line. I changed this to $fullpath and it works.

    Brilliant, excellent. Problem solved. Thanks.
     
    CleverDick, Jul 28, 2009 IP