Jump to content


Photo

Mod Rewrite Clean Urls Help Understanding


  • Please log in to reply
3 replies to this topic

#1 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 20 October 2006 - 02:43 PM

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/se...re.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=$1

Output:  http://mysite.com/seestore/1

My 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,


#2 invincible_virus

invincible_virus
  • Members
  • PipPip
  • Member
  • 27 posts
  • LocationDelhi, India

Posted 20 October 2006 - 05:07 PM

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
RewriteRule ^seestore/hats/$ seestore.php?cat_id=1
RewriteRule ^seestore/shoes/$ seestore.php?cat_id=2

PG accommodations in India - http://www.pgspace.com/
Technical Forum - http://www.enterpriseplatform.net/

#3 roopurt18

roopurt18
  • Staff Alumni
  • Advanced Member
  • 3,749 posts
  • LocationCalifornia, southern

Posted 20 October 2006 - 06:14 PM

The site I'm working on (and not originally set up by me) uses a rule like:
RewriteEngine  on
RewriteRule ab(.*) /handler.php$1 [PT]

handler.php is essentially turned into the entry point for the site and allows for urls such as:
ab/shopping/1/34/qrj

handler.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/param1/param2/param3

So let's say that only param2 and param3 are set, I still have to put a placeholder for param1:
shopping/*/34/qrj

I would much rather prefer a mechanism where the url parameters can appear in any 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.
PHP Forms : Part I | Part II

JavaScript: Singleton

http://www.rbredlau.com

#4 mcloan

mcloan
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 20 October 2006 - 09:10 PM

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 Increment
Product_id
Category Description

Tables Products:
Product_id Key Field Auto Increment
Category_id
Product Description

If 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.co..._id=1&prodID=53


RewriteRule ^seestore/hats/angels-baseball-hat$ seestore.php?cat_id=1&prodID=53

Is 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.co..._id=1&postID=35

After Mod Rewrite:  http://www.mysite.co...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.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users