开发者

htaccess mod_rewrite - beginners question

Lets say I have one file like index.php that needs to process a url like this:

index.php?cat=开发者_Go百科1&sub=2&id=3

Now I want to have nice urls for that, which will look like:

index.php/cars/bmw/x3

My question is, if the word 'cars' is the name in the database associated with the 'cat/category' id of 1, when i go to index.php/cars, how does the url access the database, find 'cars', grab it's corresponding id of '1', and map it to the url?

this site is in Php / MySQL


In your .htaccess file inside DocumentRoot of your webserver place this code:

RewriteEngine On
RewriteRule ^index.php/cars/(.+)$ /index.php?carName=$1 [QSA,L,NC]

That will call your index.php with whatever comes after /cars/ in the URL with the query parameter carName.

Then inside your index.php write a MySQL query to query the database, something like: select * from cars_table where name=$_REQUEST["carName"]; and you will be able to retrieve the matching car record.

However just as a caution SQL like above are subject to SQL injection attack so it is better to use prepared sql statement or Mysqli exetnsion of php for querying the DB instead of directly putting http request value.


You need to use RewriteMap directive

This page has an example similar to your need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜