Jump to content


This topic is now archived and is closed to further replies.


Mod Rewrite Clean Urls Help Understanding

Recommended Posts

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=$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,

Share this post

Link to post
Share on other sites
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 -
RewriteRule ^seestore/hats/$ seestore.php?cat_id=1
RewriteRule ^seestore/shoes/$ seestore.php?cat_id=2

Share this post

Link to post
Share on other sites
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:

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/[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:

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

Share this post

Link to post
Share on other sites
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
Category Description

Tables Products:
Product_id Key Field Auto Increment
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:


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.com/post.php?category_id=1&postID=35

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

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.