Php 代码点火器。htaccess
我正在使用代码Ignitter编写一个站点。这是我当前的.htaccess文件Php 代码点火器。htaccess,php,codeigniter,.htaccess,Php,Codeigniter,.htaccess,我正在使用代码Ignitter编写一个站点。这是我当前的.htaccess文件 RewriteEngine on RewriteCond $1 !^(index\.php|css|js|images|robots\.txt) RewriteRule ^(.*)$ /ci/index.php/$1 [L] 我有一个用户控制器,它可以在从url访问时显示一个配置文件,如下所示 mysite.com/user/getProfile/$username 我想做的是去掉getProfile,这样mysi
RewriteEngine on
RewriteCond $1 !^(index\.php|css|js|images|robots\.txt)
RewriteRule ^(.*)$ /ci/index.php/$1 [L]
我有一个用户控制器,它可以在从url访问时显示一个配置文件,如下所示
mysite.com/user/getProfile/$username
我想做的是去掉getProfile,这样mysitecom/user/$username将调用getProfile函数。同时仍然保留上面的代码。如果我不得不牺牲在控制器中拥有其他功能,那就这样吧
任何建议都将不胜感激 在“用户”控制器中,不要在getProfile()函数中传递$username
你会做的是。。。在默认函数中传递$username
比如说,您有一个索引函数,默认登录页是index()
在你的路由器中,你有这样的配置
$route['default_controller'] = "user/index";
$route['(:any)'] = 'user/index/$1';
在控制器类中,这应该是这样的
class User extends CI_Controller {
public function index($page = 'user')
{
if ($this->uri->segment(2) != '') {
$data['Profile'] = $this->_getProfile($this->uri->segment(2));
}
$this->load->view($page, $data);
}
private _getProfile($usernanme)
{
// This will return the profile of the user
}
}
希望这足够清楚……:) 以下是CI Url重写的示例 首先是.htaccess以摆脱index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
然后是config文件夹中controller和routes.php的简单示例
$route['default_controller'] = "main";
$route['404_override'] = '';
$route["contact"] = 'main/contact';
$route["(.*)"] = 'main/tekstovi/$1';
控制器的设计与实例
class Main extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
$this->load->view("view_home", $data);
}
public function tekstovi()
{
$data['results'] = $this->get_db->('TABELNAME',$this->uri->segment(1));
$this->load->view("view_tekstovi", $data);
}
public function contact()
{
$this->load->view("view_contact", $data);
}
}
只需编辑config.php,就可以在url的末尾添加.html
$config['url_suffix'] = '.html';
这只是一个简单的示例,请尝试在routes.php文件中添加您自己的:)添加以下内容:$route['user/(:any)]='user/getProfile/$1';我同意Ula,routes.php应该用来处理CI中的路由请求,而不是.htaccess文件。
$config['url_suffix'] = '.html';