Is there a random redirect plugin for wordpress?

zonfar

Power Member
Joined
May 12, 2009
Messages
726
Reaction score
464
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.
 
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.

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
 
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

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.
 
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.

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(); 
?>
 
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(); 
?>

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?
 
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?

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
 
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

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.
 
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.
 
I'm also looking for such a plugin! Has been made at that is still active?
 
Back
Top