简体   繁体   中英

Redirecting external URLs to an exit page

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 sort 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM