locked
URL Rewriting with RegExp RRS feed

  • Question

  • User-889949988 posted
    I have tried and tried to understand how this works, and I cannot figure out for the life me 1: Regular Expressions and 2:How to format them for URL-Rewriting.

    I'm using some code I found online that does URL-rewriting but I don't know how to set up my rules. Here's an example:
    <rule name="Search Page"><!--probably wrong...-->
    <url>/([a-zA-Z][\w-]{1,149})\/</url>
    <rewrite>/search.aspx?q=$1</rewrite>
    </rule>
    <rule name="Content Page">
    <url>/([a-zA-Z][\w-]{1,149})/([a-zA-Z][\w-]{1,149})\/</url>
    <rewrite>/content.aspx?Category=$1&Item=$2</rewrite>
    </rule>

    Here are the rules I want.
    1. I have a stylesheet and javascript files that I don't want to break when rewriting kicks in.
    2. http://www.domain.com/Default.aspx shows up as http://www.domain.com/(nothing)
    3. http://www.domain.com/search.aspx?q=search+text converts to: http://www.domain.com/s/search+text/
    4. http://www.domain.com/content.aspx?c=category&p=page converts to: http://www.domain.com/category/page/

    Is this possible/difficult? Please help, I would greatly appreciate it.
    Friday, May 11, 2007 4:50 PM

All replies

  • User-900677162 posted
     Hi, solepixel
    I am not quite sure about rewriting the Url by RegExp. However i recommend you to use custome HttpHandler to rewriting the Url.
    Here is the sample:
    using System.Web;
    public class SimpleRewriter: System.Web.IHttpModule
    {
    HttpApplication _application = null;
    public void Init(HttpApplication context)
    {
    context.BeginRequest+=new System.EventHandler(context_BeginRequest);
    _application = context;
    }
    public void Dispose()
    {
    }
    private void context_BeginRequest(object sender, System.EventArgs e)
    {
    string requesturl =
    _application.Context.Request.Path.Substring(0,
    _application.Context.Request.Path.LastIndexOf(“//”)
    );
    //Here is where we parse the original request url to determine
    //the querystring parameters for the unfriendly url
    string[] parameters = requesturl.Split(new char[] {‘/’});
    if (parameters.Length > 1)
    {
    string firstname = parameters[1];
    string lastname = parameters[2];
    //Rewrite the request path
    _application.Context.RewritePath(“~/unfriendly.aspx?firstname=” +
    firstname + “&lastname=” + lastname);
    }
    }
    }

     }

     

    As you can see from the listing, in this sample you use the BeginRequest event in the HttpModule to
    parse the incoming HTTP request path and create a new URL that you execute. Normally, when you
    click the hyperlink on friendlyurl.aspx, an HTTP request is sent to the server for execution and then
    IIS returns the page asked for in the hyperlink. In this case, you make a request for this page:
    http://localhost:1234/WebProject1/John/Smith/trickiis.aspx
    But, because you put the HttpModule in the request-processing pipeline, you can modify the HTTP
    request and change its behavior. The code in the BeginRequest method of the module parses the
    request path to create a querystring that the unfriendly.aspx page can understand and execute. So
    when you execute the code in the listing, you convert the original path into the following:
    http://localhost:1234/WebProject1/unfriendly.aspx?var1=John&var2=Smith

     

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

    Sunday, May 13, 2007 9:35 PM
  • User-889949988 posted
    1. Where do I put this code? 2. Do you have it in VB?
    Monday, May 14, 2007 9:18 AM
  • User-900677162 posted

    HI, solepixel 

    This is a whole class of custom HttpModule. You can copy and paste it without any changes to make it work. I am sorry i am not familiar with VB ,however you can use some convert tools to translate it into vb style.

    I hope the above information will be helpful. If you have any issues or concerns, please let me know. It's my pleasure to be of assistance

    Tuesday, May 15, 2007 12:00 AM
  • User-889949988 posted

    I've tried to convert this using a C# to VB converter, but get a couple of problem lines.

    Imports System.Web
    Public Class SimpleRewriter
    	Implements System.Web.IHttpModule
    Private _application As HttpApplication = Nothing
    Public Sub Init(ByVal context As HttpApplication) Implements System.Web.IHttpModule.Init
    AddHandler context.BeginRequest, AddressOf context_BeginRequest
    _application = context
    End Sub
    Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
    End Sub
    Private Sub context_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
    'Here is where we parse the original request url to determine
    'the querystring parameters for the unfriendly url
    'TODO: INSTANT VB TODO TASK: Assignments within expressions are not supported in VB.NET
    'ORIGINAL LINE: string requesturl = _application.Context.Request.Path.Substring(0, _application.Context.Request.Path.LastIndexOf("); string[] parameters = requesturl.Split(New char[] {‘/’}); if (parameters.Length > 1)
    Dim requesturl As String = _application.Context.Request.Path.Substring(0, _application.Context.Request.Path.LastIndexOf("); String() parameters = requesturl.Split(New Char() {‘/’}); if (parameters.Length > 1)
    Dim firstname As String = parameters(1)
    Dim lastname As String = parameters(2)
    'Rewrite the request path
    'TODO: INSTANT VB TODO TASK: Assignments within expressions are not supported in VB.NET
    'ORIGINAL LINE: _application.Context.RewritePath("~/Nullable(Of unfriendly.aspx)firstname=" + firstname + "&lastname=" + lastname);
    _application.Context.RewritePath("~/Nullable(Of unfriendly.aspx)firstname=" & firstname & " And lastname=" & lastname)
    End Sub
    End Class
    
    }

    I do have my URL Rewriting in place via a few files in my App_Code folder along with some stuff in the web.config file. However, I'm now having issues with forms and I guess the "dopostback" thing ASP.net uses when submitting forms. Would this code fix that problem?

    Wednesday, May 16, 2007 11:59 AM