Welcome Guest, Not a member yet? Register   Sign In
404 - Routing Problem with Home Controller - Very Strange Behaviour
#1

[eluser]Nalorin[/eluser]
Hi All,

I solved the problem below by renaming my "Home" controller to "Site", but I am still curious what the REAL problem is. My guess is just that GoDaddy.com's hosting is stupid...

~~~~~~ All of the following was written before I resolved the problem. See end of post for more info ~~~~~~

I've got a very strange behavioural problem with CodeIgniter on my website.
With my DNS pointed to my development server, everything worked as intended.
When I uploaded to the production server, and updated the DNS, I expected a few snags but nothing like this!
Note: Both the development server and the production server are running Linux

The problem I'm having:

My "Home" controller is not working as expected. Let me explain:

D: Development Server (Linux web server in a VM on my home workstation.)
P: Production server hosted by GoDaddy.com

Code:
// D: Works as Intended
// P: Doesn't Work (CodeIgniter 404 - not Apache 404)
www.example.com/home/select_area

// D: Loads Home->index(), passing "select_area" as a variable
// P: Works as the url above is *intended* to work
www.example.com/home/home/select_area

// In Home->index() for "/home/home/select_area",
echo "[".$this->uri->uri_string()."]";
// outputs: [/home/select_area]
// when it should output: [/home/home/select_area]

Any ideas on what could be wrong?


Here comes all the code that I think could possibly be relevant/related to this problem:
Code:
/* in index.php in web root */
// get URL parts and store in global constants, for easy use later on
preg_match('#^(.*\.)*([^.]+)\.([^.]+)\.([^.:]+)(\:8080)?$#', $_SERVER['HTTP_HOST'],$matches);
define('SUBDOMAIN', $matches[2]);
define('DOMAIN', $matches[3]. '.' .$matches[4]);
define('HOST_PORT', (isset($matches[5]) ? $matches[5] : ''));
define('HOST_ADDR', SUBDOMAIN.'.'.DOMAIN);

define('SITE_DIR', '/my/hosted/site/dir/html');

/* in .htaccess in web root */
addhandler x-httpd-php5 .html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [NE,L]

/* in config.php */
if (HOST_ADDR.HOST_PORT != $_SERVER['HTTP_HOST']) {
  header('HTTP/1.1 302 See Other');
  header('Location: http://'.HOST_ADDR.HOST_PORT);
  exit();
}
$config['base_url'] = 'http://'.HOST_ADDR.HOST_PORT;
$config['index_page'] = "";
$config['uri_protocol']  = "REQUEST_URI"; // only option that works on production server
$config['url_suffix'] = "/";

/* in routes.php */
$route['default_controller'] = "home"; // problem remains, even if changed to "books"


application/libraries/MY_Controller.php:
Code:
class MY_Controller extends Controller
{
  protected $data;

  function MY_Controller()
  {
    parent::Controller();
    $this->data['debug'] = 0;
    $this->data['content'] = 'home';
    $this->data['login_data'] = $this->session->userdata('login_data');
    
    if ( ! $this->session->userdata('page_limit')) $this->session->userdata('page_limit',20);

    // if city exists in the areas table, proceed with loading page
    if ($this->data['city'] = $this->area_model->get_city(SUBDOMAIN)) return;
    
    // if uri_string starts with /home/select_area, proceed with loading page
    if (strpos($this->uri->uri_string(), '/home/select_area') === 0) return;
    
    // otherwise, check if session contains last selected city
    if ($this->session->userdata('subdomain'))
    {
      // if so, redirect using that city
      redirect('http://'.$this->session->userdata('subdomain').'.'.DOMAIN.HOST_PORT);
    }
    // if we got this far, no valid area was found, so redirect to area selection page
    //redirect('/home/select_area');
  }
}


application/controllers/home.php:
Code:
class Home extends MY_Controller
{
  function index()
  {
    $this->load->view('coming_soon');
  }

  function select_area()
  {
    $args = $this->uri->segment_array();

    if (isset($args[3]) && trim($args[3]) != '')
    {
      $d = array('subdomain' => $args[3]);
      if ($this->area_model->get_city($d['subdomain']))
      {
        $this->session->unset_userdata('subdomain');
        $this->session->set_userdata($d);
        redirect("http://{$d['subdomain']}.".DOMAIN.HOST_PORT);
      }
    }

    $areas = $this->area_model->get_area_array();

    $this->data['areas'] = $areas;
    $this->data['page_title'] = 'Select Your Area';
    $this->data['content'] = 'area_select';

    $this->load->view('includes/template',$this->data);
  }
}

The page is behaving kinda like there's a rewrite rule like this:
Code:
RewriteRule ^/home(.*)$ www.example.com/$1 [L]

Some More Information About The Problem
Every controller (except Home) works fine using /controller/method/...
EVERY controller works using/home/controller/method/...

~~~~~~ After changing "Home" controller to "Site": ~~~~~~
EVERY controller works as intended as /controller/method

What do you think? Is this just GoDaddy being stupid, or is this a bug in CodeIgniter? Has anyone else had a similar problem?

My project doesn't require the default controller to be named "Home", so I am now posting this for posterity - so if someone else has a similar problem, they will see this solution

Please post if you spot a problem that I missed in the code above.

Cheers
#2

[eluser]Greg Aker[/eluser]
add a query string to index.php? in your rewrite rule. GoDaddy shared hosting doesn't support PATH_INFO.

Code:
RewriteRule ^(.*)$ /index.php?/$1 [NE,L]
-greg
#3

[eluser]Nalorin[/eluser]
[quote author="Greg Aker" date="1284515794"]add a query string to index.php? in your rewrite rule. GoDaddy shared hosting doesn't support PATH_INFO.

Code:
RewriteRule ^(.*)$ /index.php?/$1 [NE,L]
-greg[/quote]
GoDaddy Shared Hosting (GDSH) doesn't support PATH_INFO, but it does support REQUEST_URI (which, as you can see in my OP, my script is set to use).

In fact, REQUEST_URI is the only CodeIgniter URI Protocol that GDSH does support (aside from Query Strings, which I view as a workaround, not a solution). I also want to avoid Query Strings because the documentation states that they aren't fully supported by all of the CodeIgniter libraries and helpers (since most are configured to use URI segments).

The only drawback that I've found so far in using REQUEST_URI on GDSH is that, as I stated in my OP, using a controller by the name of "Home" doesn't work as expected. And it's specific to JUST THAT ONE controller. My other controllers (Books, Housing, Misc, Login, Member, News, Tips, etc) all work fine. And now that I've renamed "Home" to "Site", that one works properly, now, too!

That's why I still made my OP, even after I found the solution, to find out if this behaviour is:

A) An expected behaviour of CodeIgniter
B) A problem specific to GoDaddy
C) A problem with any Shared Hosting provider
D) A bug in the CI framework

As well as to provide others who encounter this same problem an option other than using Query Strings.

Cheers
#4

[eluser]vitoco[/eluser]
i'm not sure if it's the same, but in a different hosting i had a similar problem with 'home' controller, it was that the path of my account was '/home/.../path/to/www/' and that was causing that all the other controllers work...except the 'home' controller

Saludos
#5

[eluser]Nalorin[/eluser]
I was wondering if that could be the same problem that I was having... 'cause my GoDaddy account is located in the /home/content/.../.../.../html file path




Theme © iAndrew 2016 - Forum software by © MyBB