问题
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