Rewriting the URL

Discussion in 'ASP.NET / ASP.NET Core' started by joelnet, Jan 21, 2006.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. URL Rewrite is a method of cleaning up your URLs. For example: if you have http://site.com/orders.aspx?order_number=1234, you could use URL Rewriting to make it appear as http://site.com/orders/1234.

    You can view the article and download binary and sourcefrom http://www.codeproject.com/aspnet/URLRewriter.asp

    This control uses Regex to create rules which allows for a lot of flexibility. The rule for the above translation would look something like this...

    <rule>
    <url>/orders/(.*)</url>
    <rewrite>/order_number.aspx?order_number=$1</rewrite>
    </rule>

    The rules are setup in your web.config.

    To make this work with non-aspx page (.html, .php, .jpg, etc) you will need to make changes to IIS (described in the codeproject article).





    Joel Thoms

    DiscountASP.NET
    http://www.DiscountASP.NET
     
  2. I was able to make minor modifications to the code, allowing me to import it into my project without the need for the ThunderMain.URLRewriter in the /bin directory. This is what I did...


    I copied the Rewriter.cs into my project and commented out the following lines...

    //[assembly: AssemblyDelaySign(false)]
    //[assembly: AssemblyKeyFile(@"keyfile.snk")]
    //[assembly: AssemblyKeyName("")]
    //[assembly: AssemblyVersion("1.0.783.30976")]


    I then opened my web.config and instead of putting this in...

    <section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter, ThunderMain.URLRewriter, Version=1.0.783.30976, Culture=neutral, PublicKeyToken=7a95f6f4820c8dc3" />


    I put this in...

    <section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter" />


    After that, I copied the<urlrewrites />section into mymy web.config, created my rules and added this to my global.asax file...

    protected void Application_BeginRequest(Object sender, EventArgs e) {
    ThunderMain.URLRewriter.Rewriter.Process();
    }

    done.

    This will work in asp.net 1.x and 2.0


    Joel Thoms

    DiscountASP.NET
    http://www.DiscountASP.NET
     
  3. Joel,
    I tried to do what you suggested and I keep getting a configuration error on this line: <section name="urlrewrites" type="ThunderMain.URLRewriter.Rewriter"/>

    any ideas on why i would be getting this error. Thank you for your help.

    Paul
     
  4. Did you include the dll in the bin directory? What is the exact error message?



    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  5. Joel,
    The dll was in the bin directory. Its just a configuration error when the .cs file tries to read the values out of the config file. Hope that helps. Thank you for your help.

    Paul
     
  6. I've looked at the codeproject page and the 'And Finally' section says:

    "There's one major caveat with all this. If you want to process a request with a file extension other than .aspx such as .asp or .html, then you need to change IIS to pass all requests through to the ASP.NET ISAPI extension. Unfortunately, you will need physical access to the server to perform this, which prevents you from simply XCOPY deploying your code to an ISP."

    Can somebody paste up some code that will make this work on a classic asp page? This is exactly what I need for my site (classic asp though)
     
  7. You cannot do this with classic asp. If you are using classic asp, you will need to write an ISAPI filter, which requires installation on the server (no host will allow this).


    If you are hosting on DiscountASP.net, you can contact support to have the wildcard mapping adding to your IIS settings.



    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  8. Hi Joel.


    If I request the wildcard mapping for DiscountASP support, does this mean thatclassic ASP pageswithin the virtual web mayno longer function correctly because of differences in the way objects are handled in.NET?
     
  9. That linked initial article was written in 2002. Is there any more recent method with ASP.NET, or would it still be the same?
     
  10. Depending on how you want to do it within your application.ASP.NET 2.0has built-in url rewriting support but it lacks regular expressions support. I guess this will be available with the release of IIS 7.0 which will be soon.

    Writing your own HTTP modules is a way toget around this issue. One of the issues with HTTP module solutions is that these solutions only work on aspx files (or other files that are passed to the ASP.NET engine for evaluation). But if you are hosting on DiscountASP.net, you can contact support to have the wildcard mapping added to your IIS settings.

    You can check out http://www.urlrewriting.net/default.aspx?Language=en&amp;AspxAutoDetectCookieSupport=1






    Post Edited By Moderator (mjp) : 10/20/2006 12:03:45 AM GMT
     
  11. I stayed up late last night and came up with this poor mans solution:


    1. Creating URLS. I just use ASP.NET C# code to write out URLs like
    pr-39393-SEO-Friendly-Name-Goes-Here.aspx based on the results of my SQL queries.

    (I got the pr- idea from aspdotnetstorefront)

    2. Then when someone clicks on that URL to get them to the right page I have this code in global.asax Application_BeginRequest
     
  12. I'll try it and it works perfectly for aspx pages.
    But can I force this to work with other script languages (php for example?)
    I tried but IIS returns me text of php file, and doesn't want to process it.
    Any suggestions? Is it possible at all?
     
  13. Well this will not work at all for PHP files. It is meant to be used only by IIS and a .NET 2.0 application.

    You will have to figure out what you can capture to process a PHP file. Can't help you there.

    ---
    Patrick
    http://www.dealwithme.com
     
  14. Bruce

    Bruce DiscountASP.NET Staff

  15. I use ASP.NET 2.0
    Does it change anything? Is there any new features in 2.0, that can help to solve this problem?
     
  16. Bruce

    Bruce DiscountASP.NET Staff

    You're right, it doesn't really matter whether you are using .net 1.1 or 2.0. I was somehow thinking of using form authentication with PHP file.

    I don't think this technique with PHP file but let me check w/ Joel.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  17. I haven't tried it, but it should work if you create a wildcard mapping to the asp.net isapi in iis.



    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
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