I have two table posts & categories
post_id | post_title | post_content | post_cat
--------------------------------------------------
1 Hello World welcome to my.. 1
. .. .. ..
categories table
cat_id | cat_name | cat_parent
-----------------------------
1 News NULL
2 Sports 1
. ... ..
Let’s say current category link for news is http://domain.com/category/1/
MySQL statment
SELECT posts.post_id,
posts.post_id,
posts.post_title,
posts.post_content,
posts.post_cat,
categories.cat_id,
categories.cat_name,
categories.cat_parent
FROM posts
INNER JOIN categories
ON posts.post_cat = categories.cat_id
WHERE posts.post_cat = (int)$_GET['cat_id']
So we can get a result for post_cat = 1
According to my current database structure, how do I remove the ID but change it to be a nice slug? Example :-
Main category - http://domain.com/category/news/
Sub category - http://domain.com/category/news/sports/
Let me know a clue how script will tell News is equal 1 on post_cat column?
You can use an .htaccess file to rewrite the URL’s for you.
The entry in the
.htaccessfile would look something like this :I’ll break down the lines here to help understand whats going on and what each line means :
category– all URL’s starting with that word will be processed by the lines below.!-f!-dparseCategoryUrl.phpfile with the entire request as a parameter calledq(query) in the $_GET array. The flags at the end of the rule (L,QSA) do two things..htaccessfile will stop performing actions on the current URL.Your
parseCategoryUrl.phpfile could contain something similar to the following :The first line will split the request by slashes into an array – and the second line removes the word category from the beginning of the array (because we know that we are parsing a category URL).
The final
$requestarray, with a URL example such as :http://example.domain.com/category/news/sportsWill be this :
So you see here that you have now successfully split the URL into an array; All you have to do now is query your database and and provide the user with the correct page.
Because the
parseCategoryUrl.phppage in fact has no output, you could just use aninclude()function to insert the correct page according to the URL that was provided.SEO is about making your pages and their URL’s more informative to search engines so that a user searching the internet will be able to receive page results that are related to their search query. A search engine looking at the URL :
http://domain.com/category/1/2will not be able to extract much information. However if your URL contains (as your question requires), category information, then a search engine will be able to deduct that the specific URL is related to :
http://domain.com/category/news/– newshttp://domain.com/category/news/sports– sports newshttp://domain.com/category/blog/– blog