Codeigniter subdomain routing
Asked Answered
U

4

16

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. Creating an A record pointing to my server's ip address.
  2. Adding new rules to CodeIgniter's routes.php file.

Here is what I came up with:

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

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

Now here is the problem...

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...

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]

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 :(

Use answered 2/2, 2014 at 14:48 Comment(4)
do you have htaccess to remove index.php?Acrodrome
I do yes. Should this make a difference?Use
Just edited the OP with my htaccess file if it helps.Use
Still can't get this working, so started a bounty. You CodeIgniter gurus should be able to earn an easy +50 here ;)Use
U
5

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

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.

Use answered 5/2, 2014 at 19:35 Comment(1)
I have a question for you - does notedu.mp/blog and notedu.mp/blog/latest also show your blog?Whitby
C
1

Does blog.domain.co/blog/latest also show a 404? maybe you could also take a look at the _remap() function for your default controller. http://ellislab.com/codeigniter/user-guide/general/controllers.html#default

Basically, CodeIgniter uses the second segment of the URI to determine which function in the controller gets called. You to override this behavior through the use of the _remap() function.

Straight from the user guide,

If your controller contains a function named _remap(), it will always get called regardless of what your URI contains. It overrides the normal behavior in which the URI determines which function is called, allowing you to define your own function routing rules.

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

Hope this helps.

Chard answered 2/2, 2014 at 20:9 Comment(1)
Thanks for your answer but I still have the same problem. blog.notedu.mp triggers the function successfully, but blog.notedu.mp/latest throws an apache 404 page (not a CodeIgniter one). Any ideas?Use
F
0

have a "AllowOverride All" in the configuration file of the subdomain in apache?

without it "blog.notedu.mp/index.php/blog/latest" work perfectly, but "blog.notedu.mp/latest" no

Flemish answered 5/2, 2014 at 12:31 Comment(1)
Thanks for your answer. I have the following rule within my sites-enabled file for my domain but still 404. # Give .htaccess full override permissions <Directory /> AllowOverride AuthConfig FileInfo Indexes Limit Options=All,MultiViews </Directory>Use
D
-1
$route['latest'] = "index";

means that the URL http://blog.example.com/latest will look for an index() method in an index controller.

You want

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

Codeigniter user guide has a clear explanation about routes here

Despite answered 5/2, 2014 at 2:23 Comment(2)
Thanks for your answer. This is actually what I have in my route - I forgot to update this part of the code since my last edit. I added $route['(:any)'] = "blog/latest"; as a test also, but I still get a 404. Any ideas?Use
Can you echo out $_SERVER['HTTP_HOST'] to verify it's what you expect (blog.notedu.mp)? Also, can you check error_log and access_log?Despite

© 2022 - 2024 — McMap. All rights reserved.