【问题标题】:CodeIgniter URL Rewrite for Nginx Server用于 Nginx 服务器的 CodeIgniter URL 重写
【发布时间】:2011-09-16 08:07:00
【问题描述】:

这是我的 CodeIgniter 控制器类代码。

class View extends MY_Controller
{

    function index($number)
    {
        .....
    }
    .......
}

通过浏览器,我可以使用这个 URL 访问 View 类的 index 方法

http://localhost/view/index/12

所以,我的问题是

有没有什么有效的方法可以把网址改写成这个网址

http://localhost/view/12

我的网络服务器是 Nginx。

【问题讨论】:

  • 您应该可以直接访问localhost/view/12。它默认为 index()。
  • hmm.. 不,它在我的服务器中产生 404 错误。 -_-;不知道为什么?
  • 我认为这两个答案是正确的。它只对我有用 .htaccess

标签: php codeigniter url-rewriting nginx


【解决方案1】:

使用 Nginx 中的以下配置从所有控制器中删除尾随 index

# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
    rewrite ^/(.*)/index/?$ /$1 permanent;
}

您可以明确地从位于

的 CodeIgniter 路由文件中路由 URL

./application/config/routes.php

插入此代码。这应该适用于 Nginx 或 Apache 服务器。

// hide index from all controllers
$route['(:any)/(:any)'] = "$1/index/$2";

// hide only from View Controller
$route['view/(:any)'] = "view/index/$1";

// hide only from View with numeric parameter
$route['view/(:num)'] = "view/index/$1";

从文档中查找有关 Nginx URL Rewrite 的更多信息。希望这对您有所帮助。谢谢!!

【讨论】:

    【解决方案2】:

    默认调用index(),但如果你想为其他功能调用,你可以利用CI中的URI路由特性。

    将此添加到配置目录中的 routes.php 中。

    $route['view/(:num)'] = "view/index/$1";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      • 2012-04-21
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      相关资源
      最近更新 更多