View Full Version : Mod Rewrite - Rewrite Category Names
Ozwold
09-02-2005, 10:34 AM
Hi,
I'm trying to rewrite this domain http://www.domain.com/show-catlinks.php?mncatid=2&catid=17 to something like this http://www.domain.com/Arts/Exhibitions.
Now I've got something similar below, but I'm not quite sure if I'm nearly there with this, or perhaps I should be doing something else?
Do I need to use RewriteMap for this sort of functionality? Has anyone done something similar?
RewriteRule /[a-zA-Z_]*([0-9]+)\.html /sites.php\?artid=$1 [I,L,U]
Thanks
Ozwold.
seomike
09-02-2005, 10:50 AM
if you are going to use category names instead of category ID's to query the database for results then you need to change your mysql/sql statements
right now your sql is using the variables mncatid=2 catid=17 to get results.
to change your sql statement to match for names 1. you have to make sure that the url variables are an exact match to what name is in the database. Also you cannot have duplicate names of categories in the database or this cannot work.
The correct way to do the rewrite rule for single name files is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /somefile.php?name=$1 [L]
For 2 files that are vairalbe (like in your case)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /somefile.php?name=$1&name$2 [L]
Ozwold
09-02-2005, 11:09 AM
Thanks for the quick response. :)
In your response seomike:-
'to change your sql statement to match for names 1. you have to make sure that the url variables are an exact match to what name is in the database'
How do you mean?
Something along the lines of:
http://www.domain.com/show-catlinks.php?name=Arts&name=Exhibitions
Am I on the right track?
Thanks
Ozwold. :)
seomike
09-02-2005, 03:15 PM
you've got the right idea.
But you'll need to know a few things to make it work.
1. The name of the database table for the category names
2. The name of the columns in those tables
3. If there are duplicate names for category names
Your current varibles are
mncatid
and
catid
these are probably short for main category id and category id
your sql statements in the code are pulling data and using these ideas to narrow down to the correct data.
for example "SELECT * FROM some_table WHERE some_column_name = '$mncatid' ORDER BY somename ASC";
The important part is the "WHERE some_column_name = $mncatid"
since your url supplies the value for mncatid changing it will cause the system to fail
If you are going to use a category name instead of a category id then you'll need to change your sql WHERE clauses.
You have to know your database in order to get the correct column name.
It's easier to use urls that combine name and the numeric id.
somedomain.com/kitchens-1/dinnerware-45.htm
you get the keywords in the urls and they don't look that bad.
JUST FYI passing names only through urls is pretty advanced you have to know exactly what you are doing. combining names and id's is a heck of alot easier since you only need to change your link output and not the entire way your application queries the database.
I've been writing tutorials lately on mod rewrite this may help or it may just overwelm but here they are. www.webforgers.net
Ozwold
09-05-2005, 04:37 AM
Thanks again seomike. :)
I will definitely take your comments on board, and now I have re-jigged the code so that I have category names in the URL which enables me to perform the mod rewrite which you kindly gave me. I think you are right though to have catid within the URL as well as the cat name, makes life a lot more easier, especially when it starts to grow!
I've visited your site, and the mod rewrite tutorial looks a good read, so that some bedtime reading for me!
Ozwold. :)