PDA

View Full Version : Simple Url Rewriting, Works on Dev Server, not DASP Production Server


dalegroupco
09-28-2009, 10:34 AM
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:

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


this is the code of the called method:


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.pa th, 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:
<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:

UrlRewriter.xmsUrlRule.WriteRuleFor(HttpContext.Cu rrent.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

bruce
09-28-2009, 11:45 AM
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.

dalegroupco
09-28-2009, 12:32 PM
You'll need to setup wildcard mapping to point to asp.net.
...

If you are on IIS 7, you can setup wildcard mapping with IIS manager.

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

Aristotle
09-29-2009, 02:49 PM
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?

dalegroupco
09-30-2009, 07:14 AM
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?

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:
<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?