1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

ASP.NET URL Rewriting

Discussion in 'C#' started by MuzD, Feb 17, 2011.

  1. #1
    Hi,

    I am struggling with asp.net url rewriting. I understand I can rewrite
    
    http://www.store.com/products.aspx?category=books
    http://www.store.com/products.aspx?category=DVDs
    http://www.store.com/products.aspx?category=CDs 
    Code (markup):
    As

    http://www.store.com/products/Books
    http://www.store.com/products/DVDs
    http://www.store.com/products/CDs 
    Code (markup):

    However,

    I am not happy rewriting specific URL's such as
    http://www.store.com/displayproduct.aspx?ID=10

    to

    
    http://www.store.com/product10.aspx
    or
    http://www.store.com/10/product
    or
    http://www.store.com/product/10
    
    Code (markup):
    I prefer to have them mapped like

    
    http://www.store.com/product/iPhone-4
    or
    http://www.store.com/product/iPhone4
    or
    http://www.store.com/product/The-iPhone4
    
    Code (markup):
    In short, I would like the url's to work like they do in Wordpress
     
    MuzD, Feb 17, 2011 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am not sure what part you are confused about?

    You say you already know how to do the remapping itself as per the original code block. The preferred mapping of the product would be same but you are using the product name rather than product ID
     
    AstarothSolutions, Feb 18, 2011 IP
  3. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the response. Yes I think I know where you are coming from. I was just trying to be sure if this was the right way to do it.

    I know to have URL's without any ID would mean comparing product name or some kind of title, do you think this will take more response time as compared to ID lookup.
    Also, if I did use ID's such as

    
    http://www.store.com/product/10/iPhone-4
    or
    http://www.store.com/product/10/iPhone4
    or
    http://www.store.com/product/10/The-iPhone4
    
    Code (markup):
    Do you think it is not really an SEO friendly URL.
     
    MuzD, Feb 18, 2011 IP
  4. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    When I use the Execution plan to analyse the results of such queries with ID gives
    
    Query 1: Query Cost (Relative to batch): 40%
    
    Clustered Index Seek
    
    Code (markup):
    With Product name and no ID I get

    
    Query 1: Query Cost (Relative to batch): 60%
    
    Clustered Index Scan
    
    Code (markup):

    Thats not very good if you have lots of request coming on a busy website or do you think it is ignorable cost.
     
    MuzD, Feb 18, 2011 IP
  5. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Text searches will always be slower than integer ones, but you are already doing text searches on your category name anyway.

    You can help limit the issue by ensuring the name column is a unique index etc. Unless you are doing anything strange with pricing etc then you can also consider caching either queries or pages serverside to reduce the hit on the SQL
     
    AstarothSolutions, Feb 18, 2011 IP
  6. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the reply, I was thinking along the same lines. I am going to carry out some more experiments and see how it goes. What do you think about indexing and also making the product name as a composite primary key?
     
    MuzD, Feb 18, 2011 IP
  7. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Have to say that I personally am not a DBA and so wouldn't be able to comment and our DBA isn't in the office today to ask him.
     
    AstarothSolutions, Feb 18, 2011 IP
  8. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I did the same with one website, had the product id within the url and then just added the product title with '-' instead of spaces afterwards. The product name is basically ignored but is there purely for SEO reasons.
    e.g:
    http://www.signkings.com.au/signs/26/Rustic-U.S-Hwy-Shields/

    although I did this by setting up custom 404 errors through my host to a handler page that displays the products if found or shows the 404 if not.
     
    camjohnson95, Feb 20, 2011 IP
  9. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks a lot camjohnson.

    I was thinking of going along the same lines, however what I am curious to find out is does having different id's have an effect on the SEO.

    Also, why not just do search by product name and have no id on it. I know this might mean its going to be higher Query costs. Wordpress does it and I am sure it is not such a bad idea.
     
    MuzD, Feb 20, 2011 IP
  10. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #10
    Yes you can can do that for sure. I am unsure of whether having and id in the url would make a difference SEO-wise. I can't imagine that it would. I have similar implemented on the same website e.g:
    http://www.signkings.com.au/find/Ford/
    The only thing with using custom 404's for redirecting is to be sure to use absolute url's and absolute path's within both your server-side code and your client-side html, or you'll run into errors when finding the file.
    An example of my cusom 404 page is:
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            
    	Dim reqURL As String
    
            reqURL = Request.Url.ToString
            reqURL = Right(reqURL, Len(reqURL) - InStr(reqURL, ";"))   
            
    	Dim uri As New Uri(reqURL)
            Dim uriS() As String
    
            uriS = uri.AbsolutePath.Split("/")
            If LCase(uriS(2)) = "find" Then
                Dim s As String
                s = "Products.aspx?q=" & uriS(3).Replace("-", " ")
                Server.Execute(s)
            ElseIf LCase(uriS(2)) = "signs" Then
                Server.Execute("Products.aspx?cat=" & uriS(3))
            Else
                Response.Write("<html><head><title>Error 404 - Page not found</title></head><body><h1>Error 404 - Page Not Found</h1><p>The page that you are looking for cannot be found.</p></body></html>")
    Response.End
            End If
    End Sub
    
    Code (markup):
    I wrote this code a while ago and isn't really well written, but you get the idea. I have no idea why i have wrote the line:
    reqURL = Right(reqURL, Len(reqURL) - InStr(reqURL, ";"))
    I'll leave it just in case but it appears not to serve any purpose, so you could probably ignore that line.

    If using names without an i.d, it definately could not do any damage to your SEO, so if it is not going to decrease your performance dramatically then it can't hurt. Just be careful that when searching for names, rather then ID's that you take into account SQL injection and implement measures to reduce that risk.
     
    Last edited: Feb 21, 2011
    camjohnson95, Feb 21, 2011 IP
  11. jamieellis

    jamieellis Active Member

    Messages:
    427
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #11
    Store product IDs in a table and the pretty URLs they map to. Then use System.Web.HttpContext.Current.RewritePath() method:

    Quick and easy way is through global.asax. If you really wanted to go to town you could build your own http handler.

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    //Read DB and match requested URL - ie, [CODE]SELECT * FROM products WHERE FRIENDLYNAME = '/products/Books'
    Code (markup):
    ...
    RewritePath("new-url.aspx?product=" + results["id"].ToString());
    ...
    //Close DB
    }[/CODE]

    This is a very unrefined example but you should get the idea.
     
    jamieellis, Feb 23, 2011 IP
  12. prptl709

    prptl709 Guest

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    It's url but how to map this url in the coding.
     
    prptl709, Feb 25, 2011 IP
  13. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks for the reply. Yes, I am doing the text based mapping without any PK int fields in the url.I think you have given a very good idea about caching the pages. It is definetly something I will be doing.

    Thanks
    Muz
     
    MuzD, Feb 25, 2011 IP
  14. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thanks for the reply. Yes, I am doing the text based mapping without any PK int fields in the url.I think you have given a very good idea about caching the pages. It is definetly something I will be doing.

    Thanks
    Muz
     
    MuzD, Feb 25, 2011 IP
  15. MuzD

    MuzD Peon

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I am looking into everones reply and should come back to all very soon. Thanks everyone!
     
    MuzD, Feb 25, 2011 IP