views:

31

answers:

2

Hi to all,

i am trying to use friendly url for my categories.

Example Database

cat_id    |   parent_id    |  name     |     url
  1               0            cat1          cat1
  2               1            cat2          cat2

My approach to do is to pass the parameter cat with url value for example show.php?cat=cat1 and in .htaccess i must rewrite to /cat1

BUT what about when i want to access cat2. I want to rewrite as cat1/cat2 so the parameter is show.php?cat=cat1/cat2 and then parse the value to secure that cat2 belong to cat1.

And so on.

I am not using MVC so i have to do it on my own.

Please if any other solutions is better please advice or suggest me reading

Thank in advance.

+1  A: 

You already save the "url" in the database, I would say the simplest solution is to just put the complete category string (including parents) in that column of the database. That is:

cat_id    |   parent_id    |  name     |     url
  1               0            cat1          cat1
  2               1            cat2          cat1/cat2

Or is your question about the .htaccess syntax?

Dean Harding
I use the path instead. My question is about the technique that a i will use to achieve it.
ntan
+1  A: 

If you want to rewrite a friendly URL, you need to make it friendly first, like this... (I'm assuming cat1 would be a proper category, such as "music" or "news" for example:

www.yoursite.com/category/cat1/
show.php?cat=cat1

Note that we have "category" not "cat" - the idea of a friendly URL is that it makes sense (and maybe helps search engines to understand your page better!)

You can then rewrite the URL with a .htaccess rule.

RewriteEngine on
RewriteRule ^category/([^/\.]+)/?$ show.php?cat=$1 [L,NC,QSA]
Sohnee