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
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
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.
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.
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
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?
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.
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.
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.
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.
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.
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
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