【问题标题】:How do we program such PHP scripts?我们如何编写这样的 PHP 脚本?
【发布时间】:2011-04-30 02:09:48
【问题描述】:

有时我们会看到包含以下 URL 的网站:

somesite.com/123.php

somesite.com/124.php

somesite.com/125.php

'.php'前面的数字是Mysql Query的ID

somesite.com/123.php = somesite.com/file.php?id=123.

我注意到这些文件 [123.php,124.php,125.php] 并没有真正存储在服务器中。

我们该怎么做?

【问题讨论】:

  • Google for apache mod_rewrite。其他服务器,如 IIS,也有自己的模块来重写 url

标签: php .htaccess


【解决方案1】:

...对 apache 使用 rewriting rules,通常放在 .htaccess 文件中。

【讨论】:

    【解决方案2】:

    在您的具体示例中,您将在 .htaccess 文件中使用重写规则,如下所示:

    RewriteRule ^/somesite\.com/([0-9]+)\.php/$ file.php?id=$1 
    

    【讨论】:

      【解决方案3】:

      .htaccess 使用 mod_rewrite 重写规则的文件。

      几个链接可以帮助您入门:

      A deeper look at mod_rewrite for Apache

      URL Rewriting Guide

      A beginner's guide to URL Rewriting

      祝你好运!

      【讨论】:

        【解决方案4】:

        您必须使用一些带有 .htaccess 文件的 http 重定向(在 linux 上)

        【讨论】:

          【解决方案5】:

          您想要的是 Apache 中的 mod_rewrite 模块(如果是这种情况,则为 IIS 等效模块)和 .htaccess 文件。

          如果您好奇,文档就在这里:http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

          这里有一个示例规则,你应该做你想做的事(将anything.php重定向到index.php?id=anything):

          RewriteRule ^(.*)\.php$ index.php?id=$1 [QSA,L]
          

          【讨论】:

            猜你喜欢
            • 2015-10-21
            • 2019-11-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-06-14
            • 1970-01-01
            • 1970-01-01
            • 2011-11-17
            相关资源
            最近更新 更多