Reputation: 2205
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.
Upvotes: 0
Views: 217
Reputation: 250932
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]
Upvotes: 1
Reputation: 72658
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?
Upvotes: 1