PHP URL重写 [英] PHP URL Rewrite

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

问题描述

我有一个这样的URL。

i have a url like this.

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

有关ID号4就等于华盛顿   对于身份证号码5将等于丹佛   对于身份证号码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的:<一href="http://httpd.apache.org/docs/current/mod/mod_rewrite.html">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

可以,例如,像这样做:

You can, for example, do it like this:

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

这将,当然,导致重写规则很多,如果你有很多个城市。所以,恕我直言,更聪明的方法是将重写URI塞来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]

心连心

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

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