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

Is there a random redirect plugin for wordpress?

Discussion in 'Black Hat SEO' started by zonfar, Feb 9, 2013.

  1. zonfar

    zonfar Power Member

    Joined:
    May 12, 2009
    Messages:
    724
    Likes Received:
    450
    I'm looking for a plugin that will allow me to choose from a list of URLs to redirect to and choose one at random. When I search for a "random redirect" plugin, the only things that appear seem to be ones that are used strictly for randomly choosing posts within WP.
     
  2. metra

    metra Junior Member

    Joined:
    Jun 6, 2012
    Messages:
    156
    Likes Received:
    49
    Location:
    -. ..- .-.. .-..
    Hi zonfar,

    Unfortunately I couldn't find any Wordpress plugins to do what you wanted as well, however, there is a solution to your problem if you are willing to mess with some code.

    Here is a PHP script that redirects to your chosen URL(s) Randomly:
    PHP:
    <?php 
    if($_SERVER['HTTP_REFERER'] != ""

    $aff_array = array("http://www.RANDOMWEBSITE1.COM"
                             
    "http://RANDOMWEBSITE2.COM"
                             
    "http://RANDOMWEBSITE3.COM"); 
    header("Location: ".$aff_array[rand(0,2)]); 
    exit(); 

    ?>
    If you have a page that you would like this redirect to happen on, you could edit the page and place this PHP script in there.
    If you wanted to do something more specific, then there are ways to modify WP in order to do what you want to do.

    I know you were probably looking for a simple, 'Install and Click' plugin, but if no better solution is found anytime soon you can try to implement the above code.

    Best,
    - Metra
     
  3. zonfar

    zonfar Power Member

    Joined:
    May 12, 2009
    Messages:
    724
    Likes Received:
    450
    Thanks! I don't mind messing with code at all, I am having problems getting that code to work though. I tried adding a new page in WP and edited the "Text" which used to be called html where you could insert all your code. I also tried creating a "redirect.php" file and inserting the code and uploaded that file on my server, but it doesn't redirect.
     
  4. metra

    metra Junior Member

    Joined:
    Jun 6, 2012
    Messages:
    156
    Likes Received:
    49
    Location:
    -. ..- .-.. .-..
    Not a problem :)

    Ah, the problem is that most web browsers pass an HTTP_REFERER by default.. and this script requires it to be empty in order to even run.

    Try this Instead:
    PHP:
    <?php 
    $aff_array 
    = array("http://www.RANDOMWEBSITE1.COM",
                              
    "http://RANDOMWEBSITE2.COM",       
                             
    "http://RANDOMWEBSITE3.COM"); 
    header("Location: ".$aff_array[rand(0,2)]); exit(); 
    ?>
     
  5. zonfar

    zonfar Power Member

    Joined:
    May 12, 2009
    Messages:
    724
    Likes Received:
    450
    Perfect, thanks!! I assume I just need to change the "rand(0,2)" according to how many websites I have in the list to randomly choose between?
     
  6. metra

    metra Junior Member

    Joined:
    Jun 6, 2012
    Messages:
    156
    Likes Received:
    49
    Location:
    -. ..- .-.. .-..
    Yup, that's correct :) Just remember the count starts at 0 (Three websites are 0, 1, 2 --> between 0 and 2 ; Five would be 0, 1, 2, 3, 4 --> rand(0, 4), and so on so forth)
    And you're quite welcome, good luck with your site :D

    Best,
    - Metra
     
  7. Digital Wiz

    Digital Wiz Newbie

    Joined:
    May 21, 2014
    Messages:
    1
    Likes Received:
    0
    A little late but - Instead of using rand(0,4) for 5 websites, rand(0,2) for 3 websites etc. you can use the sizeof() or count() functions to get the size of the arrays. They return the actual number so you would have to subtract 1 so change rand(0,n) to rand(0,sizeof($aff_array)-1)

    That would work in all cases and you dont have to remember to update it when you change the number of urls.
     
  8. Bartholomew

    Bartholomew Regular Member

    Joined:
    Dec 31, 2009
    Messages:
    290
    Likes Received:
    103
    Home Page:
    Have a look in my sig if you need something more advanced that just a random redirections to a few pages. Might be an overkill for your case though.