需要帮助使用 mod_rewrite 对不同目录中的 2 个设计进行拆分测试

需要帮助使用 mod_rewrite 对不同目录中的 2 个设计进行拆分测试

为了测试新设计是否有用,我们正在对其进行 A/B 测试。但是,该设计集成了大量文件,因此我们不想不断地移动它们。

是否可以使用 mod_rewrite 来掩盖我们已将它们都移动到它们自己的子目录中的事实?

换句话说,有人访问http://www.ourdomain.com/他们会看到设计位于“public_html/old/”或“public_html/new/”,具体取决于我们在 .htaccess 中设置显示的位置。但是,他们永远不知道设计位于子目录中。

答案1

好的!将两者都移至其子目录后,您可以执行以下操作:

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

你可以触发任何mod_rewrite可以看到的东西。对于 cookies,请将其替换为上面的部分Here's where we'll handle the logic.

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]

或者GET请求查询参数:

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]

或者两者兼而有之..

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

随机的..

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

/var/www/map.txt并使用以下内容设置文件:

dirs new|old

随机性要复杂得多,我可能遗漏了一些东西。如果它坏了,请告诉我。

相关内容