Simple Url Rewriting, Works on Dev Server, not DASP Production Server

Discussion in 'Visual Studio' started by dalegroupco, Sep 28, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. Hello all,
    I am using the following code as a simple to test URL rewriting in the production environment. This code works perfectly on my dev and test machines, though it does not work at all on the DASP server, and instead the request is redirected to the custom404 page.

    in Application_BeginRequest, I call the rewriter as:

    Code:
    UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1]); 

    this is the code of the called method:

    Code:
    namespace UrlRewriter
    {
        public class xmsUrlRule
        {
            public string url { set; get; }
            public string path { set; get; }
            public string condition { set; get; }
            public string ignore { set; get; }
            
            
            public static void WriteRuleFor(string url)
            {
                xmsUrlRule rule = (from r in XElement.Load(global.GetPath("pathToFile.xml")).Elements("condition").Elements("rule")
                                   where r.Attribute("url").Value == url
                                   select new xmsUrlRule
                                   {
                                        ignore = r.Parent.Attribute("ignore").Value,
                                        condition = r.Parent.Attribute("check").Value,
                                        path = r.Attribute("path").Value
                                   }).First();
                if (rule != null)
                {
                    bool reWrite = true;
                    foreach (string ignoreDir in rule.ignore.Split(','))
                    {
                        if (ignoreDir.Contains(url))
                            reWrite = false;
                        else
                            continue;
                    }
                    if (reWrite)
                    {
                        //HttpContext.Current.RewritePath(rule.path, true);
                        //HttpContext.Current.Server.TransferRequest(rule.path, true);
                        HttpContext.Current.Server.Transfer(rule.path, true);
                        //HttpContext.Current.Response.Redirect(rule.path, true);
                        //I have tried all 4 of the above methods, none of which have worked
                    }
                }
            }
        }
    }
    
    and here is an example line from the xml file:
    Code:
    <rule url="insurance" path="commercial-insurance.aspx" />
    so basically any request for http://www.mysite.com/insurance should redirect the request to http://www.mysite.com/commercial-insurance.aspx but instead it is being caught by the custom404.aspx page. To solve this problem I searched for solutions and thought that I could add the call to the rewrite method again in the custom404.aspx pages OnInit event:
    Code:
    UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Current.Request.Url.Segments[HttpContext.Current.Request.Url.Segments.Length - 1]);
    But to no avail, the request still ends on the 404 page. Does anyone have any suggestion as to what I am doing wrong? I really am unsure what I can do to fix this issue.

    Thanks,
    Shawn
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    You'll need to setup wildcard mapping to point to asp.net.

    If you are on IIS 6, create a support ticket

    If you are on IIS 7, you can setup wildcard mapping with IIS manager.
     
  3. Do you know of a simple tutorial, or would you be able to explain how to set up wildcard mapping in IIS manager?

    Thanks,
    Shawn
     
  4. It should only be necessary to setup wildcard mapping for IIS7 applications if it is running in classic mode. With integrated mode, there is no need.

    Is your site on an IIS7 server running in integrated mode?
     
  5. I figured out a solution to the problem. Basically I created a class derived from IHttpHandler which calls the url rewriter class I have above in the ProcessRequest method.

    Next, I added:
    Code:
    <add verb="*" path="*" name="RewriteHandler" type="UrlRewriter.RewriteHandler, RewriteHandler" />
    to the <system.webServer> <handlers> section.

    Then I went into IIS manager -> HttpHandlers -> clicked view ordered list and moved this rule all the way down to the bottom of the list and it worked.

    So now to test the rewriter on my dev & test machines, I call the url rewriter class directly in App_BeginRequest and in production I comment this code out and use the method I described above.

    I'm not really sure if this is the best approach as it is merely my first implementation. Do you have any recommendations on the "right" way to implement this?
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page