简体   繁体   English

CodeIgniter URL重写Nginx服务器

[英]CodeIgniter URL Rewrite for Nginx Server

This is my CodeIgniter Controller class code. 这是我的CodeIgniter Controller类代码。

class View extends MY_Controller
{

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

Through browser, I can access the View class's index method using this URL 通过浏览器,我可以使用此URL访问View类的索引方法

http://localhost/view/index/12

So, my question is 所以,我的问题是

is there any efficient way to rewrite the URL, for example, into this URL 有没有有效的方法来重写URL,例如,到这个URL

http://localhost/view/12

My web server is Nginx. 我的网络服务器是Nginx。

index() is called by default , but if you want to do it for other functions , you can make use of URI Routing feature in CI. 默认情况下会调用index() ,但如果要对其他函数执行此操作,则可以使用CI中的URI路由功能。

Add this to routes.php in config directory. 将其添加到config目录中的routes.php。

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

Remove the trailing index from all controllers using the following configuration in Nginx 使用Nginx中的以下配置从所有控制器中删除尾随index

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

Explicitly you can route the URLs from CodeIgniter Routing file located in 显然,您可以从位于的CodeIgniter路由文件中路由URL

./application/config/routes.php

Insert this code. 插入此代码。 This should work for both Nginx or Apache Servers. 这应该适用于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";

Find more information about Nginx URL Rewrite from documentation. 从文档中查找有关Nginx URL Rewrite的更多信息。 Hope this helps you. 希望这对你有所帮助。 Thanks!! 谢谢!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM