Codeigniter 3 - 如何从URL中删除函数名称

时间:2017-07-09 05:05:47

标签: php .htaccess codeigniter

我的网址是:

example.com/controller/function/parameter
=> example.com/category/index/category_name

我需要:

example.com/category/category_name

我已经尝试过Stackoverflow提供的几个解决方案,但是它没有用。重定向到主页或404 page not found

我尝试的选项是:

$route['category'] = "category/index"; //1

$route['category/(:any)'] = "category/index"; //2

$route['category/(:any)'] = "category/index/$1";  //3

另一条路线是:

$route['default_controller'] = 'home';

htaccess文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

在配置文件中我有:

$config['url_suffix'] = '';

2 个答案:

答案 0 :(得分:2)

我不确定为什么你无法让它发挥作用。

这是我创建的一些测试代码,用于检查... 这是使用CI 3.1.5。

.htaccess - 与您拥有的相同......

控制器 - Category.php

$route['category/(:any)'] = "category/index/$1";  //3 - this works

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

<强> routes.php文件

/category/

测试网址

The Category name is None Selected 输出 /category/fluffy-bunnies

The Category name is fluffy-bunnies 输出 Private Sub CommandButton2_Click() Dim IMBacklogSh As Worksheet, logoffSh As Worksheet, deniedsh As Worksheet Set IMBacklogSh = ThisWorkbook.Worksheets("Backlog") Set logoffSh = ThisWorkbook.Worksheets("Claims Logged off") Set deniedsh = ThisWorkbook.Worksheets("Claims Denied") With IMBacklogSh If .AutoFilterMode Then .AutoFilterMode = False With .Cells(1, 1).CurrentRegion .AutoFilter field:=13, Criteria1:="#N/A" .AutoFilter field:=14, Criteria1:="C" With .Resize(.Rows.Count - 1, Columns.Count).Offset(1, 0) If CBool(Application.Subtotal(103, .Cells)) Then .Copy Destination:= _ logoffSh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) 'optionally delete the originals .EntireRow.Delete End If End With .AutoFilter field:=14, Criteria1:="<>C" With .Resize(.Rows.Count - 1, Columns.Count).Offset(1, 0) If CBool(Application.Subtotal(103, .Cells)) Then .Copy Destination:= _ deniedsh.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) 'optionally delete the originals .EntireRow.Delete End If End With End With If .AutoFilterMode Then .AutoFilterMode = False End With

玩一下,看看你是否能找到问题。

答案 1 :(得分:1)

我认为您的.htaccess文件中存在错误。请在下面找到.htaccess文件的代码。

您可以使用RewriteBase为重写提供基础。

RewriteEngine On
RewriteBase /campnew/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]

在Controller中使用您的方法。

public function index($category_name = null) {
    $this->load->model('category_model');

    $data = array();

    if ($query = $this->category_model->get_records_view($category_name)) {
        $data['recordss'] = $query;
    }
    if ($query2 = $this->category_model->get_records_view2($category_name)) 
    {
        $data['recordsc2'] = $query2;
    }

    $data['main_content'] = 'category';
    $this->load->view('includes/template', $data);
}

在模型文件中

public function get_records_view($category){
    $this->db->where('a.linkname', $category); 
}

如果不起作用,请告诉我。