Question

College B has a subscription to Website A which they access through their internal proxy, which alters all the pages URLs to accommodate redirects through their proxy.

For instance, the about us page on Website A typically looks like this:

website-a.com/about/us

if you view the page over college B's proxy connection, that link would be:

website-a.proxy-b.com/about/us

The rewrite is done on the proxy, Website A has no control over it.

This isn't at all uncommon, so I'm hoping the problem I'm looking at is an easy fix:

Website A uses a Google Custom Search Engine... The search runs after the proxy has done its URL rewriting, and the dynamic search results will return with UNALTERED URLs... so if a proxy user follows one, they are returned to our site directly, and lose any access they would have had under the proxy.

(Please assume, for simplicities sake, that I'm looking for a solution to be implemented by Website A, and that Proxy B is not managed by anyone who can change anything to make this lookup happen on the College's side)

I'm looking at rewriting the URLS after the results are returned from google using javascript... but looking at the code, I foresee some serious issues with that.

Has anyone managed to get around this, or is there a simple CSE trick to rewrite the target URLs on the fly?

Was it helpful?

Solution

Found a solution:

    function fixProxyLinks(){
        proxy_rex = new RegExp( document.location.host, "gi");
        orig_rex = new RegExp( "/www.mysite.com/", "gi");

        //Check if current server is DIFFERENT from bg.com
        if( document.location.host.search( orig_rex ) == -1 ){
            $('.gsc-results a').each( function(){
                // Find all the fresh new search results and correct them for the sake of the Proxy

                if( this.href.search( proxy_rex ) == -1 &&  this.href.search( bg_rex ) >= 0 ){
                    this.href = this.href.replace( orig_rex, '/'+document.location.host+'/' );
                    $(this).attr("data-cturl", this.href);
                    $(this).attr("data-ctorig", this.href);

                }
            });
        }
    }

And then you stick this in where you're building your CSE object: customSearchControl.setSearchCompleteCallback(this, fixProxyLinks);

NOTE: About the data-cturl and data-ctorig attributes:
Part of the CSE functionality is to REWRITE that url to redirect through google for tracking, data-cturl is the URL that it will rewrite the href to on click. Unfortunately, you have to replace the google redirect completely and just have the redirect target the same Proxy url, or Google will notice and put up a warning that you are being redirected. Once the user has clicked, CSE will write the href back to the value set in data-ctorig so you obviously want to have that match as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top