【问题标题】:PHP URL RewritePHP URL 重写
【发布时间】:2011-10-28 14:47:53
【问题描述】:

我有一个这样的网址。

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

对于 ID 号 4 将等于华盛顿 对于 ID 号 5 将等于丹佛 id 6 等于纽约

我想把网址改成这样。

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

这可能吗?

我该怎么做呢?

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:

    看看 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

    【讨论】:

    • 我认为第一个示例会起作用。它是一个列表,不是很长。我认为 stackoverflow 应该允许人们将钱用于信用和 xfer 信用给那些回答好/正确答案的人(恕我直言)
    猜你喜欢
    • 2017-09-22
    • 2011-03-17
    • 2012-01-16
    • 2018-12-03
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    相关资源
    最近更新 更多