This is my CodeIgniter Controller class code.
class View extends MY_Controller
{
function index($number)
{
.....
}
.......
}
Through browser, I can access the View class's index method using this URL
http://localhost/view/index/12
So, my question is
is there any efficient way to rewrite the URL, for example, into this URL
http://localhost/view/12
My web server is 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.
Add this to routes.php in config directory.
$route['view/(:num)'] = "view/index/$1";
Remove the trailing index
from all controllers using the following configuration in Nginx
# 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
./application/config/routes.php
Insert this code. This should work for both Nginx or Apache Servers.
// 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. Hope this helps you. Thanks!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.