locked
URL rewritiing RRS feed

  • Question

  • User431591932 posted

    Hi,

    In my application, I have two domain my.domain1.com and my.domain2.com.

    When the user calls in my.domain2.com/page , Is it possible to replace the  Address URL to My.domain1.com/page but displays the page of My.domain2.com using HttpHandlers or HttpModules.

    I cannot do much of a coding in my.domain2.com.

    Suren

     

     

     

     

    Monday, July 16, 2007 12:30 PM

Answers

  • User1001868398 posted

    You can use the load balancing that I wrote that does that:

    http://alpascual.com/blog/al/archive/2006/05/26/Load-Balancing-control-in-C_2300_-.aspx

    http://alpascual.com/blog/al/archive/2006/07/23/Http-Load-balancing-in-detail-.aspx

    Let me know if you have any questions

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 16, 2007 2:35 PM
  • User601250725 posted

    Okay, you approach could benefit from a change, here's a neat trick.

    First, when rewriting a request, do not respond with a Response.Redirect(string) if not really necessary, it's namly possible to instead pick the request handler and just hand the request off to the default aspx handler. This avoids a round-trip between the client and server, also, if the client doesn't recognize that Redirect header, you wont really send a right response. So, I would do the following to both improve you solution, and fix the problem.

    So instead of creating a new class which implements IHttpHandler inherit System.Web.UI.PageHandlerFactory and override GetHandler to map you request accordingly. Now what you do is that you call the base.GetHandler(HttpContext context, string requestType, string url, string pathTranslated) to rewrite the request and then return that handler to process the request instead.

    Since you end up with a loopback which you don't want you can use the context.Items collection to add a flag wheter to continue you loop or break. I couldn't imagine why you would have such a loop, but that should allow you to identify a loop, as well as rewrite the request without a round-trip.

    Here's some code that basically does what you need.

    public class RewriteClass: PageHandlerFactory
    {
    public override System.Web.IHttpHandler GetHandler( HttpContext context, string requestType, string virtualPath, string path )
    {
        if ( context.Items.Contains( "__loop" ) )
        {
            // Loop!
        }
        else
        {
            context.Items.Add(
    "__loop", true );
        }

        return base.GetHandler( context, requestType,
            context.Response.ApplyAppPathModifier(
    "~/somepage.aspx" ),
            context.Request.PhysicalApplicationPath );
    }
    }

    you can use this RewriteClass in Web.config as you would with a normal IHttpHandler, (NOTE: I doubt you will get the problem with the loop anymore, since you don't even have a round-trip.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 20, 2007 11:21 AM

All replies

  • User1001868398 posted

    You can use the load balancing that I wrote that does that:

    http://alpascual.com/blog/al/archive/2006/05/26/Load-Balancing-control-in-C_2300_-.aspx

    http://alpascual.com/blog/al/archive/2006/07/23/Http-Load-balancing-in-detail-.aspx

    Let me know if you have any questions

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 16, 2007 2:35 PM
  • User431591932 posted

    Hi,

    Thanks for your reply, I am still working on your code.

    suren

     

    Wednesday, July 18, 2007 9:39 AM
  • User1001868398 posted

    Let me know if you need any help

    Wednesday, July 18, 2007 10:22 AM
  • User431591932 posted

    Hi,

    Thanks for your help. But right now I am really stuck with this problem. Hopw you can help me 

    I stuck with the finding whether my page (originalpage.aspx) has just URL Refferer . If it doen't have URL Refferer, it should return to somepage.aspx. If it has URL refferer it should return to the originalpage.aspx

    Here is my code

    public void ProcessRequest(System.Web.HttpContext context)

    {

    HttpRequest objRequest = context.Request;

    String strrawURL = context.Request.RawUrl;

    String redir = System .Configuration .ConfigurationManager .AppSettings [context.Request.Url.Host];if (redir != null)

    {

    if (objRequest.UrlReferrer != null) { context.Response.Redirect(redir, true);}else { context.Response.Redirect("/OriginalPage.aspx");}

    }

    }

    public bool IsReusable

    {

    get {return true;}

    }

    I have added the below to my web.config

    <add verb="*" path="*/OriginalPage.aspx" type="MyHandler.NewHandler,MyHttpHandler"/>

     

    Now myproblem is the originalpage.aspx is getting looped, Can anyone please tell me how to come out the loop after checking once.

    Suren 

     

    Wednesday, July 18, 2007 10:55 AM
  • User601250725 posted

    Okay, you approach could benefit from a change, here's a neat trick.

    First, when rewriting a request, do not respond with a Response.Redirect(string) if not really necessary, it's namly possible to instead pick the request handler and just hand the request off to the default aspx handler. This avoids a round-trip between the client and server, also, if the client doesn't recognize that Redirect header, you wont really send a right response. So, I would do the following to both improve you solution, and fix the problem.

    So instead of creating a new class which implements IHttpHandler inherit System.Web.UI.PageHandlerFactory and override GetHandler to map you request accordingly. Now what you do is that you call the base.GetHandler(HttpContext context, string requestType, string url, string pathTranslated) to rewrite the request and then return that handler to process the request instead.

    Since you end up with a loopback which you don't want you can use the context.Items collection to add a flag wheter to continue you loop or break. I couldn't imagine why you would have such a loop, but that should allow you to identify a loop, as well as rewrite the request without a round-trip.

    Here's some code that basically does what you need.

    public class RewriteClass: PageHandlerFactory
    {
    public override System.Web.IHttpHandler GetHandler( HttpContext context, string requestType, string virtualPath, string path )
    {
        if ( context.Items.Contains( "__loop" ) )
        {
            // Loop!
        }
        else
        {
            context.Items.Add(
    "__loop", true );
        }

        return base.GetHandler( context, requestType,
            context.Response.ApplyAppPathModifier(
    "~/somepage.aspx" ),
            context.Request.PhysicalApplicationPath );
    }
    }

    you can use this RewriteClass in Web.config as you would with a normal IHttpHandler, (NOTE: I doubt you will get the problem with the loop anymore, since you don't even have a round-trip.)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 20, 2007 11:21 AM