繁体   English   中英

Codeigniter子域路由

[英]Codeigniter subdomain routing

我正在尝试在CodeIgniter框架上运行的网站上设置博客脚本。 我希望这样做,而不对我现有网站的代码进行任何重大代码更改。 我认为创建一个指向另一个Controller的子域将是最干净的方法。

我为设置新的Blog控制器所采取的步骤包括:

  1. 创建指向我服务器的IP地址的A记录。
  2. 向CodeIgniter的routes.php文件添加新规则。

这是我想出的:

switch ($_SERVER['HTTP_HOST']) {
    case 'blog.notedu.mp':
        $route['default_controller'] = "blog"; 
        $route['latest'] = "blog/latest";
        break;
    default:
        $route['default_controller'] = "main";
        break;
}

这应该将blog.notedu.mpblog.notedu.mp/latest指向我的blog控制器。

现在问题是......

访问blog.notedu.mpblog.notedu.mp/index.php/blog/latest工作正常,但访问blog.notedu.mp/latest由于某种原因带我到404页面...

我的.htaccess文件看起来像这样(从url中删除index.php的默认值):

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

我的Blog控制器包含以下代码:

class Blog extends CI_Controller {

    public function _remap($method){
        echo "_remap function called.\n";
        echo "The method called was: ".$method;
    }

    public function index()
    {
        $this->load->helper('url');
        $this->load->helper('../../global/helpers/base');

        $this->load->view('blog');
    }

    public function latest(){
        echo "latest working";
    }

}

我在这里错过了什么或做错了什么? 几天来我一直在寻找这个问题的解决方案:(

经过4天的反复试验,我终于解决了这个问题!

原来这是一个.htaccess问题,以下规则修复了它:

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

感谢所有阅读或回答此问题的人。

blog.domain.co/blog/latest也显示404吗? 也许您还可以查看默认控制器的_remap()函数。 http://ellislab.com/codeigniter/user-guide/general/controllers.html#default

基本上,CodeIgniter使用URI的第二段来确定控制器中的哪个函数被调用。 您可以通过使用_remap()函数覆盖此行为。

直接来自用户指南,

如果您的控制器包含一个名为_remap()的函数,则无论您的URI包含什么,它都将始终被调用。 它会覆盖URI确定调用哪个函数的正常行为,允许您定义自己的函数路由规则。

public function _remap($method)
    {
        if ($method == 'some_method')
        {
            $this->$method();
        }
        else
        {
            $this->default_method();
        }
    }

希望这可以帮助。

在apache的子域的配置文件中有一个“AllowOverride All”?

没有它“blog.notedu.mp/index.php/blog/latest”工作得很好,但是“blog.notedu.mp/latest”没有

$route['latest'] = "index";

表示URL http://blog.example.com/latest将在index控制器中查找index()方法。

你要

$route['latest'] = "blog/latest"; 

Codeigniter用户指南对此处的路线有清晰的解释

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM