URL conditional Rewrite?

Discussion in 'PHP' started by hasen, Apr 17, 2008.

  1. #1
    I want to program a conditional rewrite in PHP which will be located in my header.php file that redirects the URL based on a condition such as the geo location or referrer. The part I don't know how to do is to redirect in the manner I would like. For example: A user tries to access the following fictional URL:

    www.domain.com/products/

    but because the meet a certain condition, for example their geographical location I would want to redirect them to:

    www.domain.co.nz/products

    I want this to happen with one set of code for every page but it rewrites/redirects the page but only changes the top level domain (TLD) i.e .com to .co.nz

    Help would be very much appreciated. :)
     
    hasen, Apr 17, 2008 IP
  2. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    matthewrobertbell, Apr 18, 2008 IP
  3. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #3
    okay if you have geo location script.
    you must have the script to tell country TLD.
    like for canada, .ca
    for USA, .us
    for australia, .au
    for Some country, .com

    suppose you have the geolocation tld on $tld
    now suppose $tld is .ca
    now write this thing in a new file called redirect.php :
    <?php
    $current_page = str_replace(".com", $tld, $_SERVER['PHP_SELF']);
    if ($tld != '.com') {
    header("Location: $current_page");
    }
    ?>
    PHP:
    now on every page, on top of anything, write this code:
    <?php
    include_once ("Path-to-redirect.php");
    ?>
    PHP:
    where the Path-to-redirect.php is the absolute path to file, i-e: "includes/redirect.php"
    This code will redirect the user to the TLD of their country, without changing the URL , for example :
    http://example.com/members/usercp/avatars.php --> http://example.co.uk/members/usercp/avatars.php

    The above action will only be performed if the user is not in a country whose TLD is .com (which you can assign in geo location script).

    For complete guide, read this : http://www.pegor.com/tutorials/php-mysql/redirecting-users-according-to-their-country-tld/

    Hope this helps :)
     
    nastynappy, Apr 18, 2008 IP
    Bagi Zoltán likes this.
  4. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Nastynappy (what a name!) that sounds like exactly what I want. I'll have to try it. That's amazing since I thought no one was gonna reply or maybe it couldn't be done. Thanks very much for your help.
     
    hasen, Apr 18, 2008 IP
  5. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    My pleasure :)
    If you want more of these codings, request them on my website :)
    I will write them for you.
    my website is in my sig.

    Thankyou
     
    nastynappy, Apr 23, 2008 IP
  6. hasen

    hasen Peon

    Messages:
    994
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks very much. I've bookmarked your website for future use. :)
     
    hasen, Apr 23, 2008 IP