Using Codeigniter Routing - codeigniter-2

I have two controllers in my CI: teacher and exams . I have set teacher as the default controller using $route['default_controller'] = 'teacher/index' in the file routes.php. So when i type the url http://localhost:8080/New/
(New is the name of my project folder) , the default controller is loaded. but when I type http://localhost:8080/New/exams it says that The requested URL /New/exams/index was not found on this server.
What Should i do?

You call it like this
http://localhost:8080/New/index.php/exams
If you want to remove the index.php you should edit your .htaccess in the root folder :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /New/
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|stylesheets|javascript)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>

Related

cant access welcome.php in webbrowser

just set up my code igniter project using nginx as a server. I can access the welcome page at localhost , and localhost/index.php.
But I can't access the welcome page at localhost/index.php/welcome.
the browser is giving me a 404 error/file not found
Please make .htaccess file in codeigniter root project.
Then try to add this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>

Codeignitier mod_rewrite and parent folder

I am using CodeIgniter and I am trying to remove the index.php from the urls. I have mod_rewrite enabled in Apache, I set the index to a blank string in the CI config file and I am using the .htaccess file found at the bottom of the page. I think part of the problem might be that my application and system folders are not in my document root. My local server folder structure looks like this:
mysite.dev/
-public_html (server root)
-application
-system
-.htaccess
I did it that way because I read that it would be extra security having the application and system outside of the public_html folder. But the rewrite rules aren't working and I suspect it has something to do with this. I'm relatively new to this so I really appreciate any help!
my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn’t in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename ’application’ to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404′s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
I have used that code in my .htaccess file as it's working for me.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Codeigniter remove index.php

I am using CodeIgniter 2.1.1 my site structure is like
mysite/
application/
controllers/
backend/
welcome.php
welcome.php
views/
backend/
frontend/
.htaccess
and I can access backend with
http://localhost/mysite/index.php/backend
but I want
http://localhost/mysite/backend
Similarly for frontend I want
http://localhost/mysite/
In config/config.php I set
$config['base_url'] = '';
$config['index_page'] = '';
and experiment many code in .htaccess the latest one is
<IfModule mod_rewrite.c>
RewriteEngine On
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Your .htaccessshould be in the root of your site, one level above the applicationfolder.

CodeIgniter and mod_rewrite with multiple applications

I'm trying to use CodeIgniter for multiple applications along with mod_rewrite for the application folder/name.
My CodeIgniter structure is as follows:
webroot/appone/
webroot/apptwo/
webroot/system/
webroot/index.php
webroot/appone.php
webroot/apptwo.php
I followed the example listed here to use CI with multiple apps. http://codeigniter.com/wiki/Multiple_Applications/
I'm looking to use mod_rewrite that will show the following:
domain.com/appone/
domain.com/apptwo/
I believe Dirk is correct.
Here's the code I'm using (got it from Elliot Haughin) on every app folder and it works fine. Just change APP_FOLDER_NAME to whatever you wish appone, apptwo etc.
have an .htaccess file on each folder with the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /APP_FOLDER_NAME
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
PS. Don't forget to remove the index.php and define base url on CI config.
Cheers.

codeigniter 404 on existing controller

I am facing a different problem in codeigniter.
When I try to access the videos page in my site, it will redirect to 404.shtml page. But cvideos.php file exists in my controllers folder.
If I access like this http://domain-name/videos , then it will be redirected like this
//domain-name/500.shtml
And If I access the same page like this //domain-name/videos/myvideos, then then it will be redirected like this
//domain-name/404.shtml
Also, If I change the controller name from videos to some other name like videoss it works fine. Can anyone tell wats the issue.
I used this line in my .htaccess also just for testing. But no use.
RewriteRule ^videos/$ index.php/videoss/ [L]
Your controller needs to be the same name as the class it contains.
hence -
<?php
controller Videos extends Controller {
/* bla */
}
?>
should be saved as:
videos.php in the "controllers" directory.
Nothing else will work.
also your rewrite rule has two "s"'s, but that might be intentional.
and it looks like what you are trying to do with .htaccess can be achieved with CI's routing
Edit: .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
please note I did not create this - it was found by scouring the CI forums. It's a rather thorough htaccess however.
Don't forget to set the value of "index.php" to "" in your config.
base_url is case sensitive
localhost/site/controller = c:...\site
localhost/SITE/controller = c:...\SITE
make sure you are not missing .htaccess file in your root directory. Or check for its accuracy. Make sure uri_protocol and base_url are set correctly in config.php.
for sample, i am copying my .htaccess file here
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /your_dir
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
I used this to get rid of index.php in url.
I had a similar problem when transferring a CI site from WAMPP to a LAMPP that someone else had made. It would seem that WAMPP was configured to be case-insensitive, as when I converted all my controller/model/view file names and all php strings containing controller/model/view names to lowercase it worked perfectly.

Resources