mcloan Posted October 20, 2006 Share Posted October 20, 2006 I am in the process of learning php and dynamic web site building. I am trying to conceptualize how to use mod rewrite to have clean urls with keywords.The book I am reading structures most all database with a primary key auto increment. Suppose I have product an online store with product categories. Then the url for the page becomes something like:<a href=\"seestore.php?cat_id=cat_id \">$cat_title</a>In browser this becomes http://mysite.com/seestore.php?cat_id=1 (or 2,3,4 etc..)My understanding is that I can use mod rewrite to make the url like this:RewriteRule ^seestore/([0-9][0-9])/$ seestore.php?cat_id=$1Output: http://mysite.com/seestore/1My questions lies in how do I get the number out of the url? For seo I would much rather have the category name be presented in the url.For instance. http://mysite.com/seestore/hats/.I am assuming that an easy way to do this would be to structure my database with the actual category names as the category id, but is this the best and only way to accomplish this?You assistance with helping me understand this concept is much appreciated.Thank you, Quote Link to comment Share on other sites More sharing options...
invincible_virus Posted October 20, 2006 Share Posted October 20, 2006 One thing is sure that you needs a mapping of category_id to catergory_name.What I suggest is that you write different rule for each category - e.g[code]RewriteRule ^seestore/hats/$ seestore.php?cat_id=1RewriteRule ^seestore/shoes/$ seestore.php?cat_id=2[/code] Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted October 20, 2006 Share Posted October 20, 2006 The site I'm working on (and not originally set up by me) uses a rule like:[code]RewriteEngine onRewriteRule ab(.*) /handler.php$1 [PT][/code]handler.php is essentially turned into the entry point for the site and allows for urls such as:ab/shopping/1/34/qrjhandler.php parses the URL into a series of parameters that the pages can ask for and make decisions on.The nice thing to this approach is that it only requires a single rewrite rule for the entire site.The thing I don't like about how it was implemented is that it forces the parameters in the URL to appear in a specific order. For example, for the shopping page it has to be: shopping/[b]param1[/b]/[b]param2[/b]/[b]param3[/b]So let's say that only param2 and param3 are set, I still have to put a placeholder for param1:shopping/*/34/qrjI would much rather prefer a mechanism where the url parameters can appear in [i]any[/i] order and the next site I design will definately have this capability.(EDIT) I know this doesn't answer your question, but I brought it up as something to take into consideration for your site. Quote Link to comment Share on other sites More sharing options...
mcloan Posted October 20, 2006 Author Share Posted October 20, 2006 Thank you both very much for the reply. Very helpful information. invincible_virus my table structure would be something such as the following assuming I have categories that follow into products.Table Categories:Category_id Key Field Auto IncrementProduct_idCategory DescriptionTables Products:Product_id Key Field Auto IncrementCategory_idProduct DescriptionIf I am understanding correctly I would have to have a mod rewrite rule for each category and each product page. So a product page may look like this:http://www.mysite.com/seestore.php?cat_id=1&prodID=53RewriteRule ^seestore/hats/angels-baseball-hat$ seestore.php?cat_id=1&prodID=53Is this correct?Also, if I have a section of the site where users add a profile is there a more dynamic way to rewrite the url since I will not have a id upfront?For instance think of a forum. Each post has a post id and post title. Essentially, I would want to rewrite a post id with the title of the post such as below: No Mod rewrite: http://www.mysite.com/post.php?category_id=1&postID=35After Mod Rewrite: http://www.mysite.com/baseball/how-to-hit-a-baseball/The “how to hit a baseball” is the post topic. DO you have any suggestions for this?Thank you very much for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.