locked
Rewrite URL without using .aspx? RRS feed

  • Question

  • User820927210 posted
    How would you go about rewriting a URL such as: http://www.mysite.com/folder/page.aspx?query=WordnotNumber to: http://www.mysite.com/folder/WordnotNumber In other words I would like to put the querystring at the end of the URL without using (.aspx) I have seen some examples of rewriting the URL but they all seem to add the .aspx at the end. If you have any suggestions about how to tackle this I would appreciate it. Thanks, d.
    Monday, March 1, 2004 2:43 PM

All replies

  • User-1391363652 posted
    Something like this? using System; using System.Web; using System.IO; using System.Text.RegularExpressions; namespace AspNetTest { public class Redirector : IHttpModule { public void Init (HttpApplication app) { app.BeginRequest += new EventHandler (this.OnBeginRequest); } private void OnBeginRequest (object sender, EventArgs e) { HttpApplication app = (HttpApplication) sender; HttpContext ctx = app.Context; string path = ctx.Request.RawUrl.ToLower (); if (path.IndexOf ("?query=") != -1 ) { Regex re = new Regex ("(?<=\\?query=)(.*)", RegexOptions.IgnoreCase); string newPath = re.Match (path).Groups[1].Value; string vDir = path.Substring (0, path.LastIndexOf ("/")+1); // ctx.Server.Transfer (vDir + newPath + "/default.aspx"); ctx.Response.Redirect (vDir + newPath); } } public void Dispose () {} } } This is only "quick and dirty" sample. Now, if you want the URL to actually change in the browser I recommend Response.Redirect. If you do Server.Transfer make sure you're transferring to a an existing page since this method "executes" it and ends the response. Also, the URL will stay the same, i.e. the one with "?query=..."
    Tuesday, March 2, 2004 1:29 AM
  • User-85555881 posted
    Are you wanting to do this: the user types in http://www.mysite.com/folder/WordnotNumber, and behind the scenes it is rewritten to http://www.mysite.com/folder/page.aspx?query=WordnotNumber? If so, there are a couple of things you'll need to do. First you need to intercept the request, similar to Milan's post, parsing the Url and then rewriting it to the real Url. The rewrite is done by using the RewritePath method of the current HttpContext. As far as not needing the .aspx extension, this can be done by specifying a wildcard mapping in IIS. Here is a link with a little more specific information on doing this: ReWritePath / Application_BeginRequest Question.
    Tuesday, March 9, 2004 9:49 PM
  • User-1305149166 posted
    A lot of people think this easy, but I actually tried this rewritepath idea, which is very dumb stupid, and actually can not work. Here is what happened: I say: http://mysite/help I rewrite to http://mysite/gethelp.aspx Then there's a button on gethelp.aspx, when you click it, you will notice, the Url on the address bar changed to gethelp.aspx, it's not help any more. Who has solved this?
    Thursday, March 18, 2004 2:23 PM
  • User-1391363652 posted
    Every approach, be it RewritePath, Server.Transfer or Response.Redirect, has its strengths and weeknesses. From my experience I know it's better to use RewritePath if you're manupulating paths within the same directory. Otherwise you get into issues with images that have relative URLs because IE can't display them often times (which is natural because the browser looks at the URLs, no more no less). You may also experience issues with URLs if postbacks are involved (as you already know). I suggest you try Server.Transfer or Response.Redirect instead and see if they fit the bill. RewritePath is not stupid, it's just that it serves purposes that clearly don't meet your task. Or maybe you need to employ more agressive path rewriting if you want to hide the real URLs and accomodate for postbacks as well.
    Thursday, March 18, 2004 3:34 PM
  • User1356982465 posted
    MSDN Article
    Thursday, March 18, 2004 3:43 PM
  • User-1305149166 posted
    This is a damn good piece. It's a long time from last time I touch the low level code of ASP.NET.
    Thursday, March 18, 2004 4:54 PM
  • User877716070 posted
    I actually just implemented a solution like this on my own website and rolled it out yesterday and I ran into a couple of the problems that came up here. The first, about not using a ".aspx" extention ... I couldn't do it. My site is hosted on someone else's servers and I couldn't redirect all incoming requests through the ASP.NET engine so if it wasn't an .aspx extention, I couldn't run my code. The solution to this would be to map all incoming requests through IIS to ASP.NET and then have your DLL handle that. Good luck and I suggest the LARGE coffee for this. The second, about the form postback, here's the problem and how I solved it. The problem is that some knucklehead decided that the HTMLTextWriter class that generates the HTML on the Render event of the Page object should fill in the ACTION attribute of the FORM tag. It shouldn't. That's the dumbest idea in ASP.NET. What you have to do is subclass the HTMLTextWriter object to display action="" when rendering the FORM tag. Then you have to subclass the Page object and override the Render method and have it use your object rather than the original HTMLTextWriter. This way, the form's action attribute is blank and the form automatically posts back to itself. I'm at work right now and so I don't have the source code with me, but I can post if you want it.
    Friday, March 26, 2004 11:07 AM
  • User-1645088262 posted
    The MSDN article is very good and great timing for me ... I'm just finishing off a HttpModule that handles the Forms & Windows athentication differences, allows you to configure whether to do Context.RewritePath or Response.Redirect and also handles the form action / postback issue without any changes to the page code. :-)
    Friday, March 26, 2004 1:31 PM
  • User-232223085 posted
    Keep in mind that leaving your form's action blank is not garaunteed to work on all browsers nor would it pass any strict html validation such as w3c specifications.
    Thursday, April 1, 2004 1:02 PM
  • User877716070 posted
    Also keep in mind that if you don't leave the action tag blank, there's no point in rewriting URLs when you've got forms that need to postback to themselves.
    Thursday, April 1, 2004 2:28 PM