Jump to content

Mod Rewrite Clean Urls Help Understanding


mcloan

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,
Link to comment
Share on other sites

The site I'm working on (and not originally set up by me) uses a rule like:
[code]
RewriteEngine  on
RewriteRule 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/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/[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/qrj

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.
Link to comment
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
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.com/seestore.php?cat_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.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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.