【问题标题】:How to handle base url calls in CodeIgniter?如何处理 CodeIgniter 中的基本 url 调用?
【发布时间】:2016-02-02 07:34:26
【问题描述】:

我有一个带有 index 函数的默认控制器,可以用

调用

http://localhost/myproject/welcome

但我也想处理对基本 url 的调用

http://localhost/myproject/

我该怎么做?

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller
{
    public function index()
    {
        $this->load->view('welcome_message');
    }
}

编辑: 我已将 Welcome 控制器声明为默认控制器

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

这是htaccess

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

【问题讨论】:

  • 您的控制器名称在 URL 中的什么位置??
  • 本地主机/项目名称
  • 当你的默认控制器是welcome。您的网址类似于 http://localhost/welcome
  • 好的,请稍等。我在帖子中犯了一个错误
  • 也添加你的base_url

标签: php codeigniter base-url


【解决方案1】:

由于您似乎想从您的 URL 中删除 index,我建议您首先设置您的 .htaccess 来做到这一点:

 RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} -s [OR]
 RewriteCond %{REQUEST_FILENAME} -l [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^.*$ - [NC,L]
 RewriteRule ^.*$ index.php [NC,L]

第二步:

检查 CI 的 config.php 并将 $config['base_url'] 设置为您的 index.php 文件所在的位置(您说您想要 http://localhost/myproject/。不要省略尾部反斜杠。)

将您的config['index_page'] 值设置为''(空)

您已经设置了默认控制器,应该可以开始使用了。

【讨论】:

  • 谢谢,这解决了我的问题,但又创建了一个新问题。现在所有子页面都被重定向到localhost/subpage...the 项目段之间缺少。如果我删除 RewriteBase /,它会再次起作用
猜你喜欢
  • 1970-01-01
  • 2013-05-12
  • 2011-11-22
  • 2013-05-09
  • 2017-05-22
  • 2013-09-29
  • 2012-01-24
  • 2021-08-22
相关资源
最近更新 更多