我正在创建一个自定义联盟计划。我希望我的链接尽可能具有SEO友好性,因此我将使用附加到URL的Javascript哈希来发送联盟会员ID,阅读联盟会员ID,存储点击,然后301重定向到他们链接的页面太。这样我们就没有任何规范问题,每个联盟链接都通过链接汁!
现在,我该如何阅读以下网址?
www.mydomain.com/seo-friendly-url#ref=john
获取ref的哈希值并添加点击后,我将如何301将用户重新定向回
www.mydomain.com/seo-friendly-url
非常感谢任何帮助!
答案 0 :(得分:3)
片段标识符(#之后的部分)不会发送到服务器,因此任何可以发出HTTP响应的内容都无法读取它们(这是301重定向所需的)。
答案 1 :(得分:0)
URL的“哈希”部分未传递给服务器,因此您无法将此数据用于任何服务器端重定向或直接处理。但是,可以在页面加载时获取哈希并通过AJAX或重定向将其传递给服务器:
立即将用户从www.mydomain.com/seo-friendly-url#ref=john
重定向到www.mydomain.com/seo-friendly-url/ref/john
if (window.location.hash.match(/#ref=/))
window.location = window.location.href.replace('#ref=', '/ref/')
...但是,为什么不使用www.mydomain.com/seo-friendly-url/ref/john
开始并保存额外的腿部工作?另一条路径,通过AJAX,涉及在页面加载后读取哈希值并将其发送到要记录的服务器。
(注意:此代码使用generic cross-browser XMLHTTPRequest发送AJAX GET请求。替换为库的实现[如果您使用的是库]
window.onload = function () {
// grab the hash (if any)
var affiliate_id = window.location.hash;
// make sure there is a hash, and that it starts with "#ref="
if (affiliate_id.length > 0 && affiliate_id.match(/#ref=/)) {
// clear the hash (it is not relevant to the user)
window.location.hash = '';
// initialize an XMLRequest, send the data to affiliate.php
var oXMLHttpRequest = new XMLHttpRequest;
oXMLHttpRequest.open("GET", "record_affiliate.php?affiliate="+affiliate_id, true);
oXMLHttpRequest.onreadystatechange = function() {
if (this.readyState == XMLHttpRequest.DONE) {
// do anything else that needs to be done after recording affiliate
}
}
oXMLHttpRequest.send(null);
}
}