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.
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=..."
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.
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?
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.
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.
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. :-)
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.
developerJam...
Member
264 Points
61 Posts
Rewrite URL without using .aspx?
Mar 01, 2004 06:43 PM|LINK
MilanNegovan
Participant
1421 Points
296 Posts
MVP
Re: Rewrite URL without using .aspx?
Mar 02, 2004 05:29 AM|LINK
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=..."http://www.AspNetResources.com
ASP.NET With Emphasis On Web Standards
BryanJD
Member
224 Points
44 Posts
Re: Rewrite URL without using .aspx?
Mar 10, 2004 01:49 AM|LINK
cklein
Participant
1561 Points
363 Posts
Re: Rewrite URL without using .aspx?
Mar 18, 2004 06:23 PM|LINK
http://www.raincoder.com
Equal parts art and science
Email: cguo@raincoder.com
MilanNegovan
Participant
1421 Points
296 Posts
MVP
Re: Rewrite URL without using .aspx?
Mar 18, 2004 07:34 PM|LINK
http://www.AspNetResources.com
ASP.NET With Emphasis On Web Standards
PaulWilson
Contributor
3715 Points
745 Posts
ASPInsiders
Re: Rewrite URL without using .aspx?
Mar 18, 2004 07:43 PM|LINK
For the best .NET code, examples, and tools, visit:
WilsonDotNet.com, WilsonWebPortal.com, ORMapper.net
cklein
Participant
1561 Points
363 Posts
Re: Rewrite URL without using .aspx?
Mar 18, 2004 08:54 PM|LINK
http://www.raincoder.com
Equal parts art and science
Email: cguo@raincoder.com
uber1024
Participant
945 Points
187 Posts
Re: Rewrite URL without using .aspx?
Mar 26, 2004 03:07 PM|LINK
intesoft
Member
561 Points
114 Posts
Re: Rewrite URL without using .aspx?
Mar 26, 2004 05:31 PM|LINK
ASPAccelerator.NET - Fewer bytes, faster pages
ASPRedirector.NET - Put friendly URLs on your site
goblyn27
Member
176 Points
35 Posts
Re: Rewrite URL without using .aspx?
Apr 01, 2004 05:02 PM|LINK