URL rewrite PHP & pulls data from a MySql DB

北慕城南 提交于 2020-01-25 23:07:33

问题


I have a site tha is written in PHP and pulls data from a MySql DB. The site utilises query strings and I would like to map the query generated pages to the more search engine friendly page titles. For example I would like to acheive to the following mappings:

content.php?id=1 to map to /aboutus 
content.php?id=2 to map to /contactus
content.php?id=3 to map to /newfiles

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


回答1:


You need a field called permalink (or similar) in your database. e.g.

id | permalink | content |
1  | aboutus   | bra bra |
2  | contactus | bra bra |

Then use this in your .htaccess file

RewriteRule ^(.*)/ content.php?permalink=$1 [L]

What above .htaccess does is recognize your url in first level and pass it to url variable. e.g. when user type

yoursite.com/aboutus/

It will actually request to

yoursite.com/content.php?permalink=aboutus

(but user don't see it)

then you use $_GET['permalink'] to match your database and get content display on the screen.



来源:https://stackoverflow.com/questions/12135414/url-rewrite-php-pulls-data-from-a-mysql-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!