How to Randomize Home pages

We are testing different home pages for our WordPress-based site. We want people who visit to land on them randomly so we can determine how many follow through to the sign-up process in each design.

We have adword campaigns that direct to the corresponding home pages (A, B, C or D) when clicked on but the domain name is just the one, of course. If someone types the URL they’ll be directed to the one domain (Home A).

Read More

Our landing pages are as follows:

Home A: www.mydomain.com
Home B: www.mydomain.com/?page_id=119
Home C: www.mydomain.com/?page_id=132
Home D: www.mydomain.com/?page_id=153

I’m trying to do this through .htaccess but can’t get it to work. Can you please suggest a way?

We could have some person landing on “Home C” and the next person on “Home D”, next person on “Home A” and so on in a cycle. It’s not really random but it works for us as it ensures “a random person would land on a random Home page”

PS: We leave a cookie to ensure that a returning visitor who signed up would get the same Home page. The method used should also take this into account.

Thanks to all!
Miguel

Related posts

Leave a Reply

4 comments

  1. It’s not possible to randomize redirects via .htaccess. What you need to do is to redirect from within your wordpress index file.

    Here’s some example of what I would include in index.php in the beginning:

    <?php 
    
    if(empty($_GET)){//user is trying to access home page
     $homePages = array("www.mydomain.com/?page_id=119", "www.mydomain.com/?page_id=132", "www.mydomain.com/?page_id=153");
      $randomHome = "";
      if (isset($_COOKIE["randomHome"])){
          $randomHome = $_COOKIE["randomHome"];
      }
     else{
      $randomHome = $homePages[array_rand($homePages)];//get home page from a random index
      $_COOKIE["randomHome"] = $randomHome;
     }
      header("Location: " . $randomHome);
    }
    //and here goes rest of your wordpress index.php
    

    Another approach will be, not to redirect at all and trick wordpress into thinking that user is trying to access with params page_id = N.

    <?php 
    
    if(empty($_GET)){//user is trying to access home page
     $homePages = array(119, 153, 132);
      $randomHome = "";
      if (isset($_COOKIE["randomHome"])){
          $randomHome = $_COOKIE["randomHome"];
      }
     else{
      $randomHome = $homePages[array_rand($homePages)];//get home page id from a random index
      $_COOKIE["randomHome"] = $randomHome;
     }
      $_GET["page_id"] = $randomHome;//this will trick wp into thinking that user passed page_id=119 via browser, and hence random page will show
    }
    //and here goes rest of your wordpress index.php
    
  2. Additionally to a php solution, if you have access to the server’s config or the vhost config, you can create a randomized RewriteMap. So you’d have a file somewhere that looks like (let’s say it’s called home_pages.txt):

    home /
    home /?page_id=119
    home /?page_id=153
    home /?page_id=132
    

    Then you declare the map in either apache’s server or vhost config:

    RewriteMap home rnd:/path/to/file/home_pages.txt
    

    Then either in vhost, server, or the htaccess file in your document root, you can add:

    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteRule ^/?$ ${home:home} [L]
    

    This rule needs to be before whatever rules you may have in your htaccess file already.

    The first condition makes it so any internal redirect skips the rule entirely (prevents looping). Then the rule states that requests for / (the home page) invokes the rewrite map’s “home” hash (that’s why there’s a “home” in front of each URI in the file) and randomly pulls one of them and internally redirects to that one.

    The URL in the browser’s location bar remains http://example.com/ but they’ll get a random page from the “home_pages.txt” file.

  3. If you are using SQL for the site you can increment an integer for each access and module it by 4 [because of your 4 homepages in your network] and make load the homepage making calls depending on that integer.
    In this case you can have 4 different calls of the homepage, even if a user access the network from a mobile device.

  4. You might want to edit index.php with an array of URLs and a random integer from 0 to 3, that would returns randomly one of the URLs in the list.