CodeIgniter returns 404 for all routes but works - codeigniter-2

I have strange problem with CodeIgniter and routing system and can't find solution for it, so: I have several routes such as
'forum/(:num)'
=> 'forum/category/$1',
'forum/(:num)/(page:any)'
=> 'forum/category/$1/$2',
and them works, but return 404 code in header.
I mean I don't get 404 page, correct HTML returns and page's content displays correctly for my forum's categories. But I'm getting 404 in header (Network tab in Firebug), so I can't work with POST data correctly.
If I request /forum/ - 200 Ok returns, but when I trying to get routed page, I get right page, but with 404 Not Found.
I'm using PHP5.4+Apache2 on Linux host, if it will help You to give me solution.

I've found solution! Maybe it will save someone's time.
Problem wasn't in CodeIgniter, I've found solution in activation mod_rewrite. Yes, that worked, but wan't activated in Apache.
Just try to do
sudo a2enmod rewrite
and restart apache service after
sudo service apache restart
And all routed pages will return 200 Ok

None of these worked, but I did this instead:
At the end of my controller I put this:
$this->output->set_status_header('200');
and actually though that worked, the problem turned out to be a controller name that conflicted with a real directory name...

Related

How to rewrite specific urls in lighttpd

I moved a wiki install on lighttpd from https://www.example.com/wiki to a subdomain of https://wiki.example.com so I need to redirect anything wiki related to the new subdomain.
url.rewrite-once = (
"^/wiki" => "https://wiki.example.com",
)
This gives me an error 404 not found as the browser is still pointed to the old page.
In addition I would like to add a rule to handle pages people already have bookmarked such as sending
https://www.example.com/wiki/index.php?title=Main_Page
to
https://wiki.example.com/index.php?title=Main_Page
I ended up doing this:
url.redirect = ( "^/wiki/(.*)$ => https://wiki.example.net/$1",
"^/wiki/([^?]*)(?:\?(.*))?" => "https://wiki.example.net/index.php?title=$1&$2",
)
This works on 99% of the site. However there are a few forums threads that do not display correctly now because they are trying to redirect.
This one works and can view the forum normally
https://www.example.net/forums/showthread.php?796166-Wiki-Skins
This one breaks and tries to redirect
https://www.twcenter.net/forums/showthread.php?796105-Wiki-Extensions-amp-Gadgets
While Stackoverflow is a good resource, please also try reading the primary source documentation for the tool you are asking about. In this case, that is lighttpd documentation.
You might consider using mod_redirect to redirect the client. See documentation and examples at https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModRedirect

Ajax Request gets blocked in Firebug but works in Genymotion. Why?

I am trying to build an "app version" of my website (a social network).
I am using PhoneGap + jQuery Mobile (i started learning them today).
The app simply needs to retrieve new posts from the website and show them to the user. Therefore I thought a simple Ajax Request would do the job.
So, i created a php test file on the server (URL: http://www.racebooking.net/it/moto/app/get_post_test.php), which simply Echoes Alien contact SUCCESS!
I've made a simple html page in localhost (on my PC) called index.html with a div called #post-container and an AJAX request:
var root = "http://www.racebooking.net/it/moto"
$.get(root + "/app/get_post_test.php", function(data){
$("#posts-container").html(data);
});
If everything is correct, i expect to see Alien contact SUCCESS! in the post-content div.
What happens looks strange:
If i run the app from eclipse using Genymotion, everything works fine
and i see the message Alien contact SUCCESS! -> the AJAX request
went fine
If i open the index.html file on firefox, i don't see anything
and FireBug informs me that the cross-origin request was blocked.
He also tells me to activate CORS.
1) Why is that happening and how can i make FireBug work (which is better and faster for debugging)?
2) Am i following the right procedure or i am missing something?
I found the solution from this post.
I just needed to add header('Access-Control-Allow-Origin: *'); at the top of my php file.

Should I check if file exists before responding via .sendFile()?

Say I have this route handler in my Express.js app:
app.get('/files/:name', function (req, res) {
res.sendfile('path/to/files/' + req.params.name + '.html');
});
This path is used via Ajax on my site. I wonder if I should guard against situations where a file with the requested name does not exist on the server, in which case the server returns a 404 response. I can work with this, i.e. detect the 404 in the browser and act accordingly (e.g. display a message to the visitor). However, I'm not sure if this is a good approach. Is it OK to use the 404 response here, or am I supposed to try to avoid 404 responses if possible. (E.g. I could if a file with the requested name exists on the server and only use .sendFile() if it does.)
I'm worried that performing a manual check would only slow things down as .sendFile() already has this check built-in (i.e. it's possible to avoid this check and instead detect 404 responses in the browser which is what I'm doing right now and it works fine).
I would stick with returning a 404 instead, as that's the 404's purpose.I'm not sure there really is any other common/practical alternative.
Good luck.

MVC3 SSL Trouble - Can't switch from HTTPS to HTTP when SSL is not required

I'm trying to get my MVC3 site to redirect from HTTPS back to HTTP when the user browses to a page where it's not required (and they aren't logged in). I Don't want to have the load of running the whole site HTTPS but it's looking like thats the way I'll have to go.
I've been having loads of trouble with remote debug and symbols, but having gone back in time to 1985 and using message box equivalents to debug with I've come to the following conclusion:
if (filterContext.ActionDescriptor
.GetCustomAttributes(typeof(RequireHttpsAttribute), true)
.Any()
)
{
return true;
}
return false;
Always returns false.
The controller def starts as:
[FilterIP(
ConfigurationKeyAllowedSingleIPs = "AllowedAdminSingleIPs",
ConfigurationKeyAllowedMaskedIPs = "AllowedAdminMaskedIPs",
ConfigurationKeyDeniedSingleIPs = "DeniedAdminSingleIPs",
ConfigurationKeyDeniedMaskedIPs = "DeniedAdminMaskedIPs"
)]
[RequireHttps]
public class AccountController : Controller
{
And it doesn't seem to work for any actions in this controller (although they do get successfully routed to SSL).
Any suggestions? I'd love to see an answer for what I perceive as my own nubery ;)
Custom NotRequreHttpsAttribute tutorial
I use the above link post to implement my custom attribute, and redirect from https to http. Hope this helps.
My problem was discovered to be related to the bindings on the server published to. We use the same server for stage and production, and the stage https bindings were not set, so whenever it was calling an https page it was resolving into our production site (which looked the same so it was hard to spot).
Once I added a binding it was all solved. My code was ok...

Post array always empty on CodeIgniter application running on nginx

I have nginx 1.0.2 installed on Ubuntu 11 with php 5.3.5 running with FPM/FastCGI.
On a codeigniter applilcation of mine i get the following problem.
I have a simple login form with a username and password field. When I submit the form the $_POST array is empty. So is $this->input->post('username') and $this->input->post('password').
The application works fine on apache. I tried creating a simple test form on a simple php file and $_POST data worked perfectly. I can provide configuration files if needed. Any ideas ?
Update:
It finally worked. There were some redirects in my vhost config which were causing loss of post data. The working vhost config can be found here: http://codeigniter.com/forums/viewthread/90231/#455528
I think I know the issue now. The fact that it's returning 404 and processing your script tells me that the error_page is what's doing the 'rewrite' to get the request to /index.php. An error_page to a non-named location will transform the request into a GET, so the error_page 404 /index.php is doing an internal redirect to /index.php, but it's stripping the request body in the process (and the lack of an = means the status code isn't overridden by the error_page target). I'm not sure why all those ifs aren't performing the redirect (if processing in nginx is really weird, so it's a good idea to avoid them), but I think if you use a named location for your error page, that will preserve the request body and fill in $_POST. Try replacing your current error_page directive with:
location #redir {
rewrite ^ /index.php;
}
error_page 404 = #redir;
check these:
<form method="post">
you using the post method?
try running the profiler:
$this->output->enable_profiler(TRUE);
seeing any POST output there?
I was using my own PHP framework with similar URI rewrites as CI and on Nginx server had similar issues, cause my forms had actions like: action="admin/login/post" and I had to change them to end with .php action="admin/login/post.php" so they could work, but this requires you to change the URI rewrite of your CI class to replace that .php from the uri string.
I had a similar problem and i tried everything mentioned here. It was very simple when i figured it, I had not named my input variables in the form. I had missed the name parameter.
<input type="name" placeholder="Name" name="name">

Resources