条件缓存页面重定向

Conditional Cached Page Redirect

本文关键字:重定向 缓存 条件      更新时间:2023-09-26

我正在尝试根据网站访问者的选择设置条件缓存重定向。

下面是一个我希望拥有网站功能的例子(你可以通过访问vonage.com来测试):

当我访问www.vonage.com时,我可以在"个人、小企业、中端市场和企业"之间做出选择。

做出选择后,您将被发送到相关网站/子域-vonage.com、personal.vonage.com或enterprise.vonage.com.

然后,如果你试图返回到有选择的初始页面,你将无法返回,你将被重定向回你已经做出的选择。

这是怎么做到的?我想实现类似的功能。顺便说一句,我正在使用Wordpress,但这可能无关紧要,我可以在代码或.htaccess.

中弄脏我的手

可能有更好的方法,这是java脚本,而不是php,但cookie应该可以做到这一点。

页面加载时检查cookie是否存在,是否重定向,如果不重定向:

if (document.cookie.indexOf("home") >= 0) {
 // redirect them to home
}
else if (document.cookie.indexOf("business") >= 0) {
 // redirect them to business
}
else if (document.cookie.indexOf("enterprise") >= 0) {
 // redirect them to enterprise
}

如果这些都没有触发,并且用户在选择选项时没有重定向,则设置他们的cookie,例如:

// set a new cookie
expiry = new Date();
expiry.setTime(date.getTime()+(10*60*1000)); // Ten minutes
// Date()'s toGMTSting() method will format the date correctly for a cookie
document.cookie = "business; expires=" + expiry.toGMTString();
(function(href, referrer) {
    var enterprise = "http://enterprise.mysite.com" 
    var personal = "http://personal.mysite.com" 
    var is_enterprise = new RegExp(enterprise).test(href)
    var is_personal = new RegExp(personal).test(href)
    var cache = readCookie('portal')
    if (cache) return location.href = cache
    var portal = is_enterprise || is_personal
    if (portal) document.cookie = "portal=" + (enterprise || personal) + "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
    return null
})(location.href, document.referrer)

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}