What would be the best way to redirect users who request an external URL, to a sort of good-bye page?
I know I could work hard to install some sort of mod_rewrite-like module. But can't I do some so开发者_如何转开发rt of Page_OnRequest type function in global.asax or the master pages?
You could have these urls actually point to a redirection page on your own site before forwarding them on to the ultimate destination:
<a href="redirect.aspx?url=stackoverflow.com">Stack Overflow</a>
Then you could do whatever you wanted with the redirection, and it could be worked so that the redirection hid the url, encoded it, added parameters, etc.
Easiest way is to use jQuery to hook the click event on all the tags on the page:
$(function(){
$('a').click(function(e){
var proceed = true ;
var anchor = $(this) ;
var href = anchor.attr('href') ;
var isExternalUrl = CheckForExternalUrl( href ) ;
if ( isExternalUrl )
{
e.PreventDefault() ;
window.location = "outside_link.aspx?url=" + href ;
proceed = false ;
}
return proceed ;
}) ;
}) ;
Any link that gets clicked fires the event handler. External URLs will cause the page outside_link.aspx to loaded with the destination url in the query string. Internal URLs will behave normally.
Simple.
精彩评论