Simple whole-site A/B test rules for htaccess
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The rules below allow you to A/B test any page on your site by splitting traffic to one of two | |
# sub-pages depending on the timestamp when they visit. If the millisecond is an even number, | |
# they are redirect to /a/ if the millisecond is odd they go to /b/. This ensures a statisticaly | |
# even split since the probability of the second being even or odd is about 50/50. | |
# | |
# You will need to create 3 sub-pages on your site: @, a, b so you'll end up with : | |
# | |
# https://yourdomain.com/@/any-page-name | |
# https://yourdomain.com/a/any-page-name | |
# https://yourdomain.com/b/any-page-name | |
# | |
# NOTE : You can change the @, a, and b to anything you like, just be sure to update the rules accordingly. | |
# I do not recommend changing any of the other rules unless you really know what you are doing. | |
# If you /are/ an expert and can sugget simplifications or improvements to the rules, it would | |
# be greatly appreciated. | |
# | |
# The flow will be that a user comes to https://yourdomain.com/@/some-page and be redirected to | |
# either https://yourdomain.com/a/some-page or https://yourdomain.com/b/some-page | |
# | |
# So any link you use that starts with /@/any-page will be redirected to the same page name | |
# under /a/ or /b/ depending on the timestamp on their first visit. | |
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
# May be required to access sub directories | |
RewriteBase / | |
# ############################### # | |
# 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://YOUR-DOMAIN-COM/tracking/%1/$1 [cookie=ab_test_vers_match:true:YOUR-DOMAIN-COM,L] | |
# (2) If no cookie is set (new visitor) | |
# AND the current time is on an even-numbered second | |
# Rewrite to /test-option-a AND set our cookie | |
RewriteCond %{HTTP_COOKIE} !ab_test_vers=([^;]+) | |
RewriteCond %{TIME_SEC} [02468]$ | |
RewriteRule ^@/(.*)/?$ /tracking/even/$1 [cookie=ab_test_vers:even:YOUR-DOMAIN-COM,L] | |
RedirectMatch 302 ^/tracking/even/(.*/?)$ HTTPS://YOUR-DOMAIN-COM/a/$1 | |
# (3) If no cookie is set (new visitor) | |
# AND the current time is on an odd-numbered second | |
# Rewrite to /test-option-a AND set our cookie | |
RewriteCond %{HTTP_COOKIE} !ab_test_vers=([^;]+) | |
RewriteCond %{TIME_SEC} [13579]$ | |
RewriteRule ^@/(.*)/?$ /tracking/odd/$1 [cookie=ab_test_vers:odd:YOUR-DOMAIN-COM,L] | |
RedirectMatch 302 ^/tracking/odd/(.*/?)$ HTTPS://YOUR-DOMAIN-COM/b/$1 | |
# ############################### # | |
# A/B TESTING (END) # | |
# ############################### # | |
</IfModule> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment