URL rewrite with htaccess

i am beginner. someone built a website for me. it is written in php mySQL. the categories are retrieved and generated by the database. i would like to rewrite url for seo optimization. in my site there are 4 categories with url:


i would like to make it show up like this:

how can i do this, in a more simple way. is that simply changing .htaccess file, or is that related to rewrite mapping function. pls can you give me hint and exact example would be great. thanks

You know what regular expressions are?

I can imagine two ways to do what you’re asking, and I believe that using regular expressions in your .htaccess statements to redirect any URL like;
www.mysite.com/products.php?cid=x (Where x is a variable) to www.mysite.com/x
would be the easiest to manage as your website grows (because it’d be automated and you wouldn’t need to keep updating your .htaccess file everytime you add a new category), but your keyword wouldn’t appear in your new URL (unless you modified your variable names to be the keywords rather than numbers).

BUT, another way would be to use a plain, old mod_rewrite like;

# -301 Redirect-
RewriteEngine on
RewriteRule ^www\\.mysite\\.com?cid=1 www\\.mysite\\.com/apple

but you’d have to keep adding new statements to your .htaccess for each new category. The plus would be your keyword would appear in the URL, which is the whole point if you’re trying to improve SEO.

I know I’m right in theory, but please let me know if my example code is correct, because it’s been a long time since I’ve written any .htaccess statements and I’m trying to do it from memory.


Correct tool but bad code. I know you’ve been around the Apache forum before so you should know that a RewriteRule can only match the {REQUEST_URI}, not the {REQUEST_URI} - the ? delimiter - {QUERY_STRING}. Then, Redirects won’t work because they won’t match the query string, either.


That sort of code is reminiscent of some “canned programs” and messing about with those will mean that you’ve got to get into the module which generates the links and the module which converts the generated links into a database query. In “canned programs” like WordPress and ZenCart, it’s not pretty (although there are now built-in functions in WP to do that for you).

This IS a problem for mod_rewrite (if your coder “hand built” your website for you rather than used a “canned program”) but you’d actually learn something if you checked out the tutorial article linked in my signature. If you don’t understand something, you know where to find me.

Finally, it’s NOT a RewriteMap problem as that would require to you have access to the server’s configuration file (or your vhosts.conf) which is just too dangerous for a noobie (no offense - you can bring the whole server down with the slightest typographical or formatting error).



hi thanks all for your advice. SOD, i tried your suggestion it is not working . i think it is the problem that the 4 categories page, /products.php?cid=1; /products.php?cid=2; /products.php?cid=3; /products.php?cid=4; all generated from the same page, simply pull the database from the mysql. so the rewrite does not work. i would be grateful if anyone can give me some hints and example for that. it is very difficult to find in the website, but i think it is not uncommon problem thought. and DK thanks, i will study your writing. is there any parts that you think is relevant so that i can make it mess up quickly. if you give me more hint or suggestion that would be great. should i rewritecond with the .htaccess file; and also have to write some script in the php main file?

argh! Darn, I knew it!

Man, listen to DK Lynn from now on; He’s brilliant.



Well, that’s a first! Thanks for the compliment!

Others, I received a PM from vvk and responded that way. HOWEVER, I believe that it’s important for a webmaster to KNOW what his code is doing and continue to recommend a visit to my signature’s tutorial. It is long (especially with all the sample coding problems) but it HAS been very helpful to members over the years.