URL rewrite PHP & pulls data from a MySql DB

2019-09-15 01:55发布

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条回答
欢心
2楼-- · 2019-09-15 02:05

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.

查看更多
登录 后发表回答