Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter isn't routing right

I installed Apache for Windows. I bought CodeIgniter Professional and downloaded their source code. It said that I should put its .htaccess in the root folder of the website so I did. I set base URL to be http://127.0.0.1/kids/ where kids is the root folder of the website. It showed the homepage just fine. When I clicked on a link, it always gives the 404 error. It said, "The requested URL /index.php/welcome/cat/2 was not found on this server." I tried several possiblities of .htaccess in directories with no success. Inside the .htaccess, it instructs:

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

Explain why it's not working at all? I also checked configs.php and it seems ok as well. config['index_page'] is set to blank. I removed the .htaccess file and still same problem except that it said, "The requested URL /kids/welcome/cat/2 was not found on this server."

Hours have been wasted on trying to get it working and I am losing patience.

Here's the instruction from CodeIgniter Professional:

"To make those folders accessible to CodeIgniter, you will also need to create a small .htaccess file in the root folder of your web site. Here's what that .htaccess file would look like. It basically contains instructions that allmvs certain files and folders to be viewable on the web site:

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

If you are familiar with mod_rewrite in Apache, you will note that this rule ostensibly removes index.php from the URL of any destination. Below when you're configuring CodeIgniter (and particularly the config.php file), you'll see why this is a good idea. If you're not familiar with mod_rewrite in Apache, don't be too intimidated by this file. Basically, the .htaccess file sets forth a few rules, one of which removes index.php from all URLs. Other rules in the file allow the use of various other folders that you will need going forward (such as folders that contain CSS, JavaScript, Captcha files, and images)."

I did just as instructed... put the htaccess file in root folder and yet, it gave 404 error. If I remove htaccess file from the root folder, it shows homepage. If I add htaccess file, it gives 404 error. I moved htaccess and it didn't do anything to re-route. Obviously, the Apache understood the htaccess otherwise it wouldn't show the 404 error.

like image 709
netrox Avatar asked Aug 04 '10 01:08

netrox


People also ask

How does route work in CodeIgniter?

Routing rules are defined in routes. php file at the location application/config. In this file you'll see $route array, it permits you to specify your own routing criteria. Routes can be classified in two ways, either using Wildcards or Regular Expressions.

How do you call a route in CI?

You could achieve it by first set a router config item within the config file ( the default file is located at application/config/config. php ), for example : $config['routes']['insertUser'] = 'users/add'; then supply the config above into the standard routes item on routes.

What is routing in CodeIgniter?

Routing – routing is responsible for responding to URL requests. CodeIgniter Routing matches the URL to the pre-defined routes. If not route match is found then CodeIgniter throws a page not found exception. Controllers – routes are linked to controllers. Controllers glue the models and views together.

Where is .htaccess file in CodeIgniter?

You should place your . htaccess file at your root directory not Inside the application folder. Save this answer.


1 Answers

It's possible that your routes aren't resolving correctly, but I'm not sure. I'd recommend the following steps.

First, I would just do away with using PATH_INFO for your CodeIgniter routing, as it's not necessary. Change your .htaccess file to this:

RewriteEngine On

RewriteCond $0 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^.*$ index.php [L]

Then, in your configuration, make sure that $config['uri_protocol'] is set to AUTO (the default, I believe) or REQUEST_URI.

Following that, the most likely cause is that your routes expect there to be a trailing slash on the URL. I don't know enough about CodeIgniter to know where that particular setting is configured, but you can cause your URLs to have a trailing slash with the following configuration directive:

$config['url_suffix'] = "/";

Edit: Make sure that mod_rewrite is working, as well. Try this at the top of your .htaccess file, after the RewriteEngine directive:

RewriteRule .* http://stackoverflow.com/ [L,R]

If you get redirected to Stack Overflow, at least we'll know that's working. If not, you aren't getting a 500 error, so the problem would lie with the .htaccess file, and in that case you should confirm that the corresponding <Directory> entry in your server configuration for where your site is located on disk has AllowOverride All set.

Edit: I should have known better about the forward slash, sorry about that. The problem occurs because of how mod_rewrite works in your per-directory (.htaccess) context.

Once your URL is rewritten, it is examined to determine what mod_rewrite should do with it. When your URL points to a local resource and contains a leading slash, mod_rewrite leaves it as-is. When it does not, it adds the per-directory prefix.

When it's left as-is, an internal redirect to /index.php, in this case, is assumed to be a fully-qualified URL from the host (localhost). So, the resource requested ends up being http://localhost/index.php. When the per-directory prefix is added, a later step in the rewrite engine attempts to remove the DocumentRoot from the path, if it's present. This leaves us with /kids/index.php, which is then passed to the internal redirect, and is resolved as expected.

like image 187
Tim Stone Avatar answered Sep 29 '22 21:09

Tim Stone