PHP URL 重写 [英] PHP URL Rewrite

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

问题描述

我有一个这样的网址.

http://www.somesite.com/community.php?id=4

对于 id 号 4 将等于华盛顿身份证号码 5 将等于丹佛对于 id 号 6 将等于纽约

for id number 4 would equal Washington for id number 5 would equal Denver for id number 6 would equal New York

我希望 url 像这样重写.

I would like the url to be rewritten like this.

http://www.somesite.com/washington  for id = 4
http://www.somesite.com/denver for id = 5

这可能吗?

我该怎么做?

推荐答案

看看 mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Have a look at mod_rewrite: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

例如,您可以这样做:

RewriteEngine on
RewriteRule washington community.php?id=4 [L,QSA]
RewriteRule denver community.php?id=5 [L,QSA]

如果你有很多城市,这当然会导致很多重写规则.因此,恕我直言,更聪明的方法是将 URI slug 重写为 community.php 并从某种数据库中查找 ID:

This would, of course, lead to a LOT of rewrite rules if you have much cities. So, a IMHO smarter way would be to rewrite the URI slug to community.php and lookup the ID from some sort of database:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCOnd %{REQUEST_FILENAME} !-d
RewriteRule /?([a-z-]+) community.php?city=$1 [L,QSA]

HTH

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

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