Welcome Guest, Not a member yet? Register   Sign In
Every CodeIgniter page being returned falsely as 404, although displaying correct content
#11

[eluser]besson3c[/eluser]
I think at least part of my problem relates to my PHP setting to allow headers to be sent after the initial page headers, as the problem with /blog was that there was no corresponding WordPress page called blog, so I think once it was called it returned 404 headers internally within WordPress...

I just need to figure out my routes.php file and I think I'm set!
#12

[eluser]besson3c[/eluser]
[quote author="besson3c" date="1268008774"][quote author="tomcode" date="1268006737"]If You don't want to have 'main' in Your URL, use _remap() in Your Controller together with routing :

Code:
$route['(:any)'] = "main/$1";

If You use more than one controller, do a forum seach for more detailed examples, here's one example[/quote]



Cool, this works and gets rid of my 404 errors, but I'm having a problem with my other routes in my routes.php file:

Code:
$route['(:any)'] = "main/$1";
$route['main/learnmore/:any/ajaxreload'] = "main/learnmorecontent";
$route['main/support/category/:any'] = "main/support";
$route['main/support/:any'] = "main/supportinner";
$route['main/searchsupport/:any'] = "main/searchsupport";
$route['main/searchblog/:any'] = "main/searchblog";
$route['main/:num/:num/:any'] = "main/blogpost";

is the first rule read and the others ignored? If you want to see this in action, this is available on http://dev1.netmusician.org[/quote]




Okay, I think I can answer my own question now... Here is how I understand the design of CI, please correct me if I'm wrong:

What I would do in this case is to take out all of my routes and build in this logic to my _remap() function. I don't believe that routes.php was designed to handle double routes or routes to routes. Plus, this could get confusing.

I can see how the _remap() function would be a great, clean way to obtain precise control over your site when you want to hide the controller from within the URL as I do. However, it sort of seems like you have to make a choice between relying on _remap() or routes.php in my specific case where I want additional routing rules beyond the initial remap, and of course the .htaccess file is relevant too. In my particular case, it seems a little simpler to simply add the question mark to my .htaccess file rewrite rule so that I can continue to drop my default controller from my URL by way of my .htaccess, and I can continue to use my routes.php file.

For sites with multiple controllers you could have multiple routes in routes.php as per the link to the thread above, or multiple rewritecond arguments in an .htaccess. I suppose there is some debate over which would be more elegant.

This is sort of hard to put into words, having three means of rewriting is very powerful but certainly leads to several design decisions... Is my logic (or, at least, as much as can be followed here) sound?
#13

[eluser]tomcode[/eluser]
Yeah, You got the general idea.

The default CI behaviuor is : controller/method/arguments

The moment You want to change that it starts getting less transparent.

I use .htaccess solely to take out index.php and error page handling. I also develop without .htaccess and add the file only in the end.


I then use routing to take out the default controller name and the language token for multi language sites :
Code:
// common opening conditions
$route['default_controller'] = "default_controller_name";
$route['scaffolding_trigger'] = "";

// for multi language sites, a set for each controller
$route['([a-z]+)/controller_name'] = 'controller_name';
$route['([a-z]+)/controller_name(:any)'] = 'controller_name/$1';

// for single language sites, a set for each controller
$route['controller_name'] = 'controller_name';
$route['controller_name(:any)'] = 'connection/$1';

// common closing condition
$route['(:any)'] = "default_controller_name/$1";

The rest I handle in the Controller, usually I have a MY_Controller with centralized URI logic, etc.

This stays pretty straight forward and does not give me headaches when I look at it in a year or so. I like to have functionality grouped into one place / file. Wink
#14

[eluser]besson3c[/eluser]
[quote author="tomcode" date="1268014328"]Yeah, You got the general idea.

The default CI behaviuor is : controller/method/arguments

The moment You want to change that it starts getting less transparent.

I use .htaccess solely to take out index.php and error page handling. I also develop without .htaccess and add the file only in the end.


I then use routing to take out the default controller name and the language token for multi language sites :
Code:
// common opening conditions
$route['default_controller'] = "default_controller_name";
$route['scaffolding_trigger'] = "";

// for multi language sites, a set for each controller
$route['([a-z]+)/controller_name'] = 'controller_name';
$route['([a-z]+)/controller_name(:any)'] = 'controller_name/$1';

// for single language sites, a set for each controller
$route['controller_name'] = 'controller_name';
$route['controller_name(:any)'] = 'connection/$1';

// common closing condition
$route['(:any)'] = "default_controller_name/$1";

The rest I handle in the Controller, usually I have a MY_Controller with centralized URI logic, etc.

This stays pretty straight forward and does not give me headaches when I look at it in a year or so. I like to have functionality grouped into one place / file. Wink[/quote]




Yeah, I can definitely see the elegance in this, I may end up copying this model. What is your take on this? I handle various AJAX calls by including an /ajaxreload in the URL. For example /learnmore/whatever/ajaxreload, and I have a route:

Code:
$route['main/learnmore/:any/ajaxreload'] = "main/learnmorecontent";

I then look for the ajaxreload in the URL within my Controller's constructor, set an internal variable $this->something['ajaxreload'] = true, and then use this variable to know which views to display and how to handle this request. However, these requests also generate 404 errors.

In other words, shouldn't a functioning route in routes.php to an existing function in a valid controller not generate a 404? How would I correct this?

Thanks for all of your collective efforts to unscramble my brain Smile


Could a good argument be made for CodeIgniter to not display a page no matter what if a 404 header is generated? These sorts of problems should probably be sorted out before a site is put into production, but they are also easy to go unnoticed.
#15

[eluser]besson3c[/eluser]
[quote author="tomcode" date="1268008753"]Your're loading more than 30 js files, more than 10 css files, the hidden contact form, I do not call that optimized[/quote]



I'm still using YUI, but now that my pages (except for my AJAX calls) are not generating 404s, this content is being cached by my web browser now, so this will definitely help!
#16

[eluser]tomcode[/eluser]
I did several sites with full browser flash, similar to ajax, where I had a separate set of controllers under a folder xml, no need for routing there at all, I had only the index loaded as HTML page. Haven't done yet a full ajax driven site with multiple controllers, in a mono language situation my multi language setup would do it.

..

As far as I know, CI creates 404 headers only when sending the pages of the show_404() or show_error() pages , show_error() has the header only since CI 1.7.2, as far as I remember.
If You have Your usual content together with a 404 header, the header comes probably from somewhere else, Apache or WordPress.

I recommend You to do create an empty app with only Your routes and Controllers with empty methods. Check whether all addresses work as expected. Then add Your .htaccess to take out index.php and check that. Then Your fixed.

I never had 404 headers with content delivered other than Apache's error page or CI's error page. You really should trace it down and get rid of that.
#17

[eluser]besson3c[/eluser]
[quote author="tomcode" date="1268017724"]I did several sites with full browser flash, similar to ajax, where I had a separate set of controllers under a folder xml, no need for routing there at all, I had only the index loaded as HTML page. Haven't done yet a full ajax driven site with multiple controllers, in a mono language situation my multi language setup would do it.

..

As far as I know, CI creates 404 headers only when sending the pages of the show_404() or show_error() pages , show_error() has the header only since CI 1.7.2, as far as I remember.
If You have Your usual content together with a 404 header, the header comes probably from somewhere else, Apache or WordPress.

I recommend You to do create an empty app with only Your routes and Controllers with empty methods. Check whether all addresses work as expected. Then add Your .htaccess to take out index.php and check that. Then Your fixed.

I never had 404 headers with content delivered other than Apache's error page or CI's error page. You really should trace it down and get rid of that.[/quote]



You're right, I was sort of halfway through testing this when I read this Smile

With /ajaxreload on the end of the URL, even though WP happily fetches the right content via query_posts(), it still returns that 404 if that particular page doesn't exist in WordPress. Since /ajaxreload is just an indicator for me and will never be a part of an actual WP URL, I just stripped it out of $_SERVER['REQUEST_URI'] within my theme's functions.php file.

I think I'm squared away now, and I suppose it is not CodeIgniter's fault for not dealing with 404s that are generated by other libraries. I should really work towards disabling that PHP option that allows headers to be sent after the initial page headers to eliminate this sort of confusion.

Thanks for your help!
#18

[eluser]besson3c[/eluser]
For those of you coming to this thread late, perhaps when it is too old to bump, I've written up a summary of my findings and experiences here: http://www.netmusician.org/2010/03/codeigniterurls feel free to comment there.

I don't mean to shill the blog, I just know that it is sometimes annoying to find a thread that describes the problem you are having to a T, only for it to be closed or the solution not posted there. If it's not cool, I apologize in advance, feel free to delete this.




Theme © iAndrew 2016 - Forum software by © MyBB