如何使用 .htaccess 或 CI 路由重写网站的特定 url
http://www.domain.com/user_controller/newfunction/username
作为
$route['/(:any)'] = "/user_controller/newfunction/$1";
或尝试 htaccess
RewriteEngine On
RewriteRule ^([^/]*)$ /user_controller/newfunction/$1 [L]
更容易使用位于 application/config/routes.php 的 CI 路由系统
$route['(:any)'] = 'user_controller/newfunction/$1';
不要忘记在 application/config/config.php 中设置你的基本 url
$config['base_url'] = 'http://www.domain.com/';
添加记得删除 url 中的 index.php。在站点根目录编辑 .htaccess 文件
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|_searches|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /index.php/$1 [L]
现在在您的 user_controller 中,函数 newfunction 将如下所示:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
public function __construct() {
parent::__construct();
// load things like models, libraries, helpers
}
function newFunction($slug)
{
echo 'hello my name is'.$slug;
}
}
当您访问http://www.domain.com/nash时,您的代码应该打印出 nash
/application/config/routes.php
$route['/all/other/routes/must/come/first!'] = "wherever/you/want";
$route['/(:any)'] = "/user_controller/newfunction/$1";