使用PHP我怎么做一个重写规则? [英] Using PHP how do I make a rewrite rule?

查看:252
本文介绍了使用PHP我怎么做一个重写规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用mod-rewrite的第一次。请帮我看看这些规则

我想我的网址重写页面如下:

list.php的?城市达拉斯= >>> 列表/城市/达拉斯

profile.php?ID = 12 >>> 个人资料/ zaknuman (从数据库中检索用户名)

story.php?ID = 33 >>> 故事/那里 - 是 - 没有蚂蚁功能于得克萨斯州(从数据库中检索故事标题)


解决方案

 重写规则列表/城市/([A-ZA-Z])$ list.php的?城市= $ 1

应在范围内的每个字符匹配-Z和最后的斜线后,A-Z。

另外两个我相信你会需要嵌入蛞蝓('zaknuman'和'有,有,无蚁功能于得克萨斯州')中的数据库,然后你就可以从检索蛞蝓数据库,让你的ID的方式,可见:

 重写规则简介/(.*)$ profile.php?蛞蝓= $ 1重写规则故事/(.*)$ story.php?蛞蝓= $ 1

这最后的2不敌最后的斜杠之后的每一个字符。

编辑:不要忘记,以确保你有RewriteEngine叙述的在.htaccess文件或者Apache的配置

Using mod rewrite for the first time. Please help me with these rules

I'd like my urls rewritten for pages as follows:

list.php?city=dallas >>> list/city/dallas

profile.php?id=12 >>> profile/zaknuman (username retrieved from db)

story.php?id=33 >>> story/there-are-no-ants-in-texas (story title retrieved from db)

解决方案

RewriteRule list/city/([a-zA-Z])$ list.php?city=$1

which should match every character in the range a-z and A-Z after the final slash.

The other two I believe you'll need to embed the slug ('zaknuman' and 'there-are-no-ants-in-texas') in the database and then you'll be able to retrieve that slug from the database and get your ID that way, vis:

RewriteRule profile/(.*)$ profile.php?slug=$1

RewriteRule story/(.*)$ story.php?slug=$1

These last 2 match every character after the final slash.

EDIT: Don't forget to make sure you have "RewriteEngine On" in your .htaccess file or Apache configuration!

这篇关于使用PHP我怎么做一个重写规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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