我有一个这样的 url。
http://www.somesite.com/community.php?id=4
对于 id 号 4 等于华盛顿 对于 id 号 5 将等于丹佛 对于 id 号 6 将等于纽约
我希望 url 被重写为这样。
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
例如,您可以这样做:
RewriteEngine on
RewriteRule washington community.php?id=4 [L,QSA]
RewriteRule denver community.php?id=5 [L,QSA]
如果您有很多城市,这当然会导致大量重写规则。因此,恕我直言,更聪明的方法是将 URI slug 重写为 community.php 并从某种数据库中查找 ID:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCOnd %{REQUEST_FILENAME} !-d
RewriteRule /?([a-z-]+) community.php?city=$1 [L,QSA]
HTH
关于PHP URL 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7930849/