URL 重写 PHP &从 MySql 数据库中提取数据 [英] URL rewrite PHP & pulls data from a MySql DB

查看:65
本文介绍了URL 重写 PHP &从 MySql 数据库中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 PHP 编写的站点,并从 MySql 数据库中提取数据.网站利用查询字符串,我想将查询生成的页面映射到更多搜索引擎友好的页面标题.例如,我想实现以下映射:

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

我怎样才能以更简单的方式做到这一点.是简单地改变 .htaccess文件,或者是与重写映射函数相关的.请你能给我提示吗确切的例子会很棒.谢谢

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 need a field called permalink (or similar) in your database. e.g.

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

然后在您的 .htaccess 文件中使用它

Then use this in your .htaccess file

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

.htaccess 上面所做的是在第一级识别您的 url 并将其传递给 url 变量.例如当用户输入

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/

它实际上会请求

yoursite.com/content.php?permalink=aboutus

(但用户看不到)

然后您使用 $_GET['permalink'] 匹配您的数据库并在屏幕上显示内容.

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

这篇关于URL 重写 PHP &从 MySql 数据库中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆