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
最初に余分な足の作業を節約するために使用してみませんか?もう1つのルートは、AJAXを介して、ページがロードされた後にハッシュの値を読み取り、それをサーバーに送信して記録することです。
(注:このコードは、汎用クロスブラウザー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);
}
}