PHP URL 重写

标签 php .htaccess url-rewriting

我有一个这样的 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/

相关文章:

asp.net - 复杂重定向 : HTTP to HTTPS + port on localhost and naked domain to www,(本地主机除外)

使用常量代替字符串的 PHP 性能

同一页面上的 PHP 表单验证提前显示

php - 获取个人资料的 Facebook 创建日期

php - Laravel 5.1 强制非 WWW 和 HTTPS 不工作

带有 urlencode 和 "&"错误的 PHP $_GET var

PHP5 框架 : Autoloading and Opcode Caching

.htaccess - 重定向到 .htaccess 中的 https 文件夹

.htaccess - .htaccess 中 PDF 和图像文件的规范标题链接

apache - URL重写和域名: how to make it single slash?后的双斜杠