0

We're trying to set up a proof of concept for a/b testing using htaccess on a homepage based on this gist: https://gist.github.com/iconifyit/c8d715d62b6e3960696ac1c9cbd231c5

We've modified the code to target the homepage only & it half works, meaning that it will only use the first condition & ignores the second. I'm not 100% on my htaccess rules & I've gone over this for the past couple days to figure out what is going wrong but I feel like I'm shooting at a target in the dark.

# ############################### #
#        A/B TESTING (START)      #
# ############################### #

# (1) Check if our cookie is already set.
#     If so, redirect to the previously-viewed page.

RewriteCond %{HTTP_COOKIE} ab_test_vers=([^;]+)
RewriteRule ^/$ HTTPS://example.com/%1/$1 [cookie=ab_test_vers_match:true:example.com,L]

# (2) If no cookie is set (new visitor)
#     AND the current time is on the first half of the minute
#     Rewrite to /test-option-a AND set our cookie

RewriteCond   %{HTTP_COOKIE} !ab_test_vers=([^;]+)
RewriteCond   %{TIME_SEC} <30
RewriteRule   ^/$ /even/$1 [cookie=ab_test_vers:even:example.com,L]
RedirectMatch 302 ^/$    HTTPS://example.com/welcome/

# (3) If no cookie is set (new visitor)
#     AND the current time is on the second half of the minute
#     Rewrite to /test-option-a AND set our cookie

RewriteCond   %{HTTP_COOKIE} !ab_test_vers=([^;]+)
RewriteCond   %{TIME_SEC} >29
RewriteRule   ^/$ /odd/$1 [cookie=ab_test_vers:odd:example.com,L]
RedirectMatch 302 ^/$    HTTPS://example.com/about/

# ############################### #
#        A/B TESTING (END)        #
# ############################### #

What should be happening is that based on the time that a visitor hits the homepage of said site, they're automatically redirected to one of the two pages, in this case the pages being used for proof of concept are /welcome & /about. What's actually happening is that they're only being redirected to /welcome & the second rule is being ignored completely.

Jeff W
  • 147
  • 1
  • 2
  • 13
  • 2
    _“We've modified the code to target the homepage only”_ – when you configure rewriting in .htaccess, the path RewriteRule matches against never starts with a leading `/`, that has been stripped off at this point already. `RewriteRule ^/$` is never going to match anything in that context, you need to use `RewriteRule ^$` to target an _empty_ (relative) path here. – 04FS Oct 16 '19 at 08:31
  • @04FS it's obvious that I'm out of my depth in htaccess, I'm definitely much stronger in CSS than anything else - I modified the RewriteRule sections like you mentioned but we're still seeing the same behavior so I think I must have butchered this one pretty good or, the more likely possibility, I'm missing something SUPER freaking obvious that's staring me in the face ... I'll play with it some more to see if I can figure out what I've missed. – Jeff W Oct 16 '19 at 18:18
  • @04FS - it was quite literally staring me right in the face. With your feedback & the example that iconifyit provided to Birender, I was able to make this work. Many thanks for taking a minute to help a n00b out, post it up as the answer & I'll accept it! – Jeff W Oct 16 '19 at 18:34
  • There's a bit of a snag with this, though, it seems to have broken our checkout page, so we need to rethink this :/ – Jeff W Oct 16 '19 at 21:36

0 Answers0