doukuangxun5382 2014-12-07 01:02
浏览 51
已采纳

Codeigniter .htaccess重写数据库

I am having difficulties with Codeigniter. I am trying to get data from a MySQL data by passing the record ID as part of the URL

The URL is to be

localhost/site_folder/page/page_title/2

In the above URL, page is the name of the controller and 2 is the primary ID of the record in the database (this could be any number from 1 to 9999).

My controller includes this:

public function index()
{
    $this->load->helper('url');
    $this->load->model('pages_model');

    $id = $this->uri->segment(3,1);
    if (empty($id))
    {
        show_404();
    }

    $data['page'] = $this->pages_model->get_page($id);
    $this->load->view('page',$data);
}

My .htaccess contains this

    RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L] 

When I enter localhost/site_folder/page/page_title/2 into the address bar, it throws a 404.
Yet, when I enter localhost/site_folder/page it shows the default database entry as shown in the second value of segment(3,1) above.

So, how should I change the .htaccess file for a workable rewrite?
I have tried the following, but none worked for me:

  • RewriteRule .* page/$0 [PT,L]
  • RewriteRule .* page/(*.)/$0 [PT,L]
  • RewriteRule .* page/(?*.)/$ [PT,L]
  • 写回答

1条回答 默认 最新

  • doulian7305 2014-12-08 07:30
    关注

    You can try using the _remap function as described in the CI documentation https://ellislab.com/codeigniter/user-guide/general/controllers.html

    The _remap function if exists in a controller is the a function that is called before any class method, and in this function you can check params sent to the function and according to this call any method of the controller.

    For your example as i assume that page_title is dynamic you can either set a regular expression to check it or as i n the following example check if the method does not exists then treat it as a page title (this means that you must be sure there can not be a page title and a method name in this controller with the same name)

    public function _remap($method, $params = array())
    {
        if (!method_exists($this, $method))
        {   
            // assume this is a page_title and run the index method
    
            $this->index($method, $params);     
        }
        else {
            // means that method exists then run that method
            $this->$method( $params);
        }
    
    }
    

    Also remember that means that you should take into consideration in the index method usage of 404 header when someone just type random string that the controller will treat as a page_title.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上