【问题标题】:PHP code to alter or change URL用于更改或更改 URL 的 PHP 代码
【发布时间】:2012-03-13 17:57:21
【问题描述】:

有谁知道我可以如何更改我的网站网址的架构?我使用 Apache 服务器和 php 5.3 我有一个输出 url 的站点,如下所示

http://localhost/index.php?articleId=23

我希望它像

一样输出 url
http://localhost/articles/hot-news-of-the-day

请给我一个 url 以供参考,以获取教程、pdf 甚至是示例代码。你也可以给我一个示例代码。任何事物。请!!!

【问题讨论】:

标签: php html apache http .htaccess


【解决方案1】:

您想查看 URL 路由

类似的帖子位于PHP Application URL Routing

【讨论】:

    【解决方案2】:

    我认为您正在寻找mod_rewrite 之类的东西。这是一个 link 教程。

    【讨论】:

      【解决方案3】:

      查看 apache mod_rewrite 模块:

      Beginners guide to mod_rewrite

      Apache rewriting guide

      【讨论】:

        【解决方案4】:

        在您网站文件夹根目录的 .htaccess 文件中,您可以使用以下规则:

        RewriteEngine On
        RewriteCond %{SCRIPT_FILENAME} !-f
        RewriteCond %{SCRIPT_FILENAME} !-d
        RewriteRule ^.+$ /index.php [L]
        

        并使用将您的 URL 参数与相应功能相匹配的路由脚本。更简单的是,尝试使用可以为您执行此操作的框架,例如 Codeigniter

        【讨论】:

          【解决方案5】:

          由于您使用的是 apache,因此您需要 .htaccess 文件(或者您可以将其直接放入配置中,它会具有更好的性能)。

          我建议至少研究一下AllowOverride 的工作原理(如果你想使用.htaccess)。

          您将使用mod_rewrite,主要和最重要的命令是RewriteRule(尽管您可以使用我们重写条件)。您还应该在谷歌上搜索mod_rewrite tutorials,但您的.htaccess 文件的基本示例如下:

          # Turn on rewirting
          RewriteEngine On
          
          # Not file, not directory
          RewriteCond %{SCRIPT_FILENAME} !-f
          RewriteCond %{SCRIPT_FILENAME} !-d
          
          # map /articles/* to index.php?article_title=*
          RewriteRule ^/articles/(.+)$ /index.php?article_title=$1 [L]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-06-16
            • 2012-01-08
            • 1970-01-01
            • 2021-10-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多