codeigniter子域路由 [英] Codeigniter subdomain routing

查看:273
本文介绍了codeigniter子域路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安装上的codeIgniter框架运行一个网站的博客脚本。我想这样做而不做任何重大的code改变我现有的网站code。我想,创建一个子域名指向另一个控制器将是这样做的清洁方法。

I'm trying to setup a blog script on a website running on the CodeIgniter framework. I want do this without making any major code changes to my existing website's code. I figured that creating a sub domain pointing to another Controller would be the cleanest method of doing this.

这是我走上安装我的新博客控制器涉及的步骤:

The steps that I took to setup my new Blog controller involved:

  1. 在创建A记录指向我的服务器的IP地址。
  2. 添加新的规则,以codeIgniter的 routes.php文件文件。
  1. Creating an A record pointing to my server's ip address.
  2. Adding new rules to CodeIgniter's routes.php file.

下面是我想出了:

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.mp blog.notedu.mp/latest 我的博客控制器。

This should point blog.notedu.mp and blog.notedu.mp/latest to my blog controller.

现在这里的问题...

Now here is the problem...

访问 blog.notedu.mp blog.notedu.mp/index.php/blog/latest 作品正常,但是访问 blog.notedu.mp/latest 带我到一个404页出于某种原因...

Accessing blog.notedu.mp or blog.notedu.mp/index.php/blog/latest works fine, however accessing blog.notedu.mp/latest takes me to a 404 page for some reason...

我的.htaccess文件看起来像这样(默认为从URL中移除的index.php):

My .htaccess file looks like this (the default for removing index.php from the url):

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

和我的博客控制器包含以下code:

And my Blog controller contains the following code:

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";
    }

}

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

What am I missing out on or doing wrong here? I've been searching for a solution to this problem for days :(

推荐答案

4天之后的试验和错误,我终于解决了这个问题!

After 4 days of trial and error, I've finally fixed this issue!

原来这是一个htaccess的问题,以下规则固定的:

Turns out it was a .htaccess problem and the following rules fixed it:

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

感谢大家读或回答了这个问题。

Thanks to everyone that read or answered this question.

这篇关于codeigniter子域路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆