Click here to Skip to main content
15,886,069 members
Articles / Web Development / ASP.NET
Tip/Trick

Get search key word from the referrer url.

Rate me:
Please Sign up or sign in to vote.
4.93/5 (17 votes)
15 Nov 2010CPOL 70K   20   7
This is a very simple way to do actually. Web developers are very much familiar to work with the referrer url. The HTTP_REFERER server variable return the referrer url. In this tips I will show how easily we can get the search key word from the referrer url.
Well, lets take an example, you have a web site and the people come to your site from a search engin (Google, Bing) and now you want to know which word are used for the search and which search engin are used as well.

C#
public string GetSearchKeyWords(string strQuery)
{
    string result = "";
    string pattern = "\\b\\w*p=(?!u)\\w*\\b|\\b\\w*q=(?!u)\\w*\\b|\\b\\w*qs=(?!u)\\w*\\b"
            + "|\\b\\w*encquery=(?!u)\\w*\\b|\\b\\w*k=(?!u)\\w*\\b\\b\\w*qt=(?!u)\\w*\\b"
            + "|\\b\\w*query=(?!u)\\w*\\b|\\b\\w*rdata=(?!u)\\w*\\b|\\b\\w*search_word=(?!u)\\w*\\b"
            + "|\\b\\w*szukaj|terms=(?!u)\\w*\\b\\b\\w*text=(?!u)\\w*\\b|\\b\\w*wd=(?!u)\\w*\\b|\\b\\w*words=(?!u)\\w*\\b";
    foreach (Match m in Regex.Matches(strQuery, pattern)) {
        // get the matched string
        string x = m.ToString();
        x = x.Substring(1, x.Length - 1);
        // collect all text
        result += x;
    }
    return result.Replace("=", "");
}

Input: http://www.google.com/url?sa=t&source=web&cd=7&ved=0CEQQFjAG&url=http%3A%2F%2Fwww.example.com%2F&rct=j&q=codeproject.com=uSLVTMm0JYXNhAewqbzjBA&usg=AFQjCNFtfSFIuj4fxnl8bD_bR2NgzDePGw&cad=rja

Output: 
codeproject.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralI have just tried this script and it is only retuning the fi... Pin
Guerrilla12313-Mar-11 14:35
Guerrilla12313-Mar-11 14:35 
GeneralRe: Yes you are absolutely right...the url is valid and contain ... Pin
Md. Marufuzzaman15-Mar-11 9:16
professionalMd. Marufuzzaman15-Mar-11 9:16 
GeneralReason for my vote of 5 Great tip :-) Pin
thatraja27-Nov-10 23:18
professionalthatraja27-Nov-10 23:18 
GeneralReason for my vote of 5 Usefull Info Pin
baskarcts23-Nov-10 6:00
baskarcts23-Nov-10 6:00 
GeneralReason for my vote of 5 This is a good program. Pin
Lakshmi Narayana198722-Nov-10 17:44
Lakshmi Narayana198722-Nov-10 17:44 
GeneralReason for my vote of 5 great Pin
Evren Yortuçboylu22-Nov-10 1:43
Evren Yortuçboylu22-Nov-10 1:43 
GeneralReason for my vote of 5 Good Work Pin
Abdul Quader Mamun15-Nov-10 23:46
Abdul Quader Mamun15-Nov-10 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.