Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

In order to test if a new design is going to be useful, we're doing A/B testing on it. However, the design integrates a large number of files so we don't want to have to keep moving them around.

Is it possible to use mod_rewrite to mask the fact that we've moved both into their own subdirs?

In other words, someone visits http://www.ourdomain.com/ and they see the design located in "public_html/old/" or "public_html/new/" depending on which we have set to show in .htaccess. However, they never know the designs are in subdirs.

share|improve this question
    
What condition do you want to use to select which site they see? –  Shane Madden Aug 11 '12 at 0:27
    
Honestly, not sure how this would work so not sure what my options are on conditions. Was originally planning to just switch them manually at various intervals, but if an interval could be set in .htaccess or have it switched with a GET parameter, that would be nice, too. –  eComEvo Aug 16 '12 at 20:34
    
There's a lot of options - what would be the ideal behavior for you? –  Shane Madden Aug 16 '12 at 21:44
    
Probably best for it to trigger by IP during initial testing, then to either trigger by a cookie value so user is unaware or via a GET value. –  eComEvo Aug 17 '12 at 4:16

1 Answer 1

up vote 0 down vote accepted

Ok! After you've moved both into their subdirs, you can do something like this:

<VirtualHost *:80>
    ServerName example.com
    # .. any other needed config, logging, etc

    # Yes, you'll leave this at the parent directory..
    DocumentRoot /var/www/public_html
    <Directory /var/www/public_html>
        Order Allow,Deny
        Allow from all

        RewriteEngine On
        # Let's prevent any rewrite of anything starting with new or old,
        # to head off potential internal redirect loops from pass-thrus..
        RewriteRule ^(new|old) - [L]

        # Here's where we'll handle the logic.
        # This will vary the page based on IP address.
        # If the user is in the 10.0.0.0/8 address range...
        RewriteCond %{REMOTE_ADDR} ^10\..*$
        # ...then we'll give them the new page.
        RewriteRule ^(.*)$ new/$1 [L]
        # Otherwise, we'll give them the old page.
        RewriteRule ^(.*)$ old/$1 [L]
    </Directory>
</VirtualHost>

You can trigger off of anything that mod_rewrite can see. For cookies, swap this in for the part under Here's where we'll handle the logic. above:

RewriteCond %{HTTP_COOKIE} newsite=true
# Client sent a cookie with "newsite=true"
RewriteRule ^(.*)$ new/$1 [L]
# No matching cookie, give them the old site
RewriteRule ^(.*)$ old/$1 [L]

Or GET request query parameters:

RewriteCond %{QUERY_STRING} newsite=1
# Client sent /path/to/page?newsite=1 - give them the new one
RewriteRule ^(.*)$ new/$1 [L]
# Fall back to the old one
RewriteRule ^(.*)$ old/$1 [L]

Or for both..

RewriteCond %{QUERY_STRING} newsite=1 [OR]
RewriteCond %{HTTP_COOKIE} newsite=true
RewriteRule ^(.*)$ new/$1 [L]
RewriteRule ^(.*)$ old/$1 [L]

Randomized..

# We'll use a cookie to mark a client with the backend they've been stuck to, 
# so they they don't get fed /index.html from /old then /images/something.jpg
# from /new. Handle that first..
RewriteCond %{HTTP_COOKIE} sitevers=(old|new)
RewriteRule ^(.*)$ %1/$1 [L]

# They didn't have a cookie, let's give them a random backend.
# Define our mapping file, we'll set this up in a minute..
RewriteMap siteversions rnd:/var/www/map.txt
# Since we need to use the random response twice (for the directory and the
# cookie), let's evaluate the map once and store the return:
RewriteCond ${siteversions:dirs} ^(.*)$
# ..then, use what we just stored to both select the directory to use to
# respond to this request, as well as to set a cookie for subsequent requests.
RewriteRule ^(.*)$ %1/$1 [L,CO=sitevers:%1:.example.com:0]

And set up the /var/www/map.txt file with these contents:

dirs new|old

Random is a lot more complicated, and I might have missed something.. let me know if it breaks.

share|improve this answer
    
Wow! This is great! Is there a way to have it randomly select which rewriterule to use? –  eComEvo Aug 17 '12 at 15:11
    
@EcomEvolution Added a random selection config. Untested, so let me know if it breaks and we'll work through getting it right. –  Shane Madden Aug 17 '12 at 18:07
    
Much appreciated! Far more than I had ever expected. :) Problem with the design has set things back, so it'll probably be next week before we get to try this out. –  eComEvo Aug 18 '12 at 23:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.