Unable to route to static PHP page in CodeIgniter - codeigniter

I'm a newbie to CodeIgniter. In my 'views' folder, I've created 2 PHP pages - home.php and register.php. I've also created the header and footer PHP pages in a templates folder.
Here is my pages.php code which is the controller class:
<?php
class Pages extends CI_Controller
{
public function view($page)
{
if(!file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
show_404();
}
$data['title'] = ucfirst($page);
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
and here is my routes.php code:
$route['register'] = "pages/view/register";
$route['default_controller'] = 'pages/view/home';
I have not yet written anything into the .htaccess file.
My project home directory is 'ED'
I am able to navigate to the home page with this URL: http://localhost/ED
However I'm unable to navigate to the registration page with these URLs:
localhost/ED/register
or
localhost/ED/register.php
Please help as to how can I achieve this.

You have to have the index.php by default because according to CodeIgniter URLs
By default, the index.php file will be included in your URLs:
example.com/index.php/news/article/my_article
So if you want to remove the index.php, you can follow this simple step:
You can easily remove this file by using a .htaccess file with some
simple rules. Here is an example of such a file, using the "negative"
method in which everything is redirected except the specified items:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP
request other than those for index.php, images, and robots.txt is
treated as a request for your index.php file.
Just put your .htaccess file(with the mod rewrite) in you main appplication folder and voila it's done.
What I did with my .htaccess was this one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /name_of_your_codeigniter_folder/
#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>

Related

Codeigniter a href redirecting to localhost WAMP Server

I'm new in CodeIgniter and trying to create a simple link on Homepage but it is redirecting to localhost on WAMP Server, here is my code
autoload.php in config folder
$autoload['helper'] = array('html', 'url');
config.php in config folder
$config['base_url'] = 'http://localhost:8080/OTI-CI';
routes.php in config folder
$route['default_controller'] = 'site';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
here is my controller: site.php
class Site extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->view("header");
$this->load->view("content_home");
$this->load->view("footer");
}
}
here is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /OTI-CI/
#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
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>
here is my view of header include a link like
Home
is there anything I'm missing about it?
In CodeIgniter 3.x version, you should capitalize Controllers, Libraries and Models name. Like that Site.php
Wrong:
/application/controllers/site.php
Right:
/application/controllers/Site.php

Using Codeigniter Routing

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>

my route in codeigniter does not work for dynamic pages

my page url is
http://example.com/ci/pages/portofolio
My roots in config/routes.php
$route['default_controller'] = "index";
$route['pages/(:any)'] = 'display/display_pages/$1';
$route['404_override'] = '';
My controller in controllers/display.php
class display extends MY_Controller{
function __construct(){
parent::__construct();
}
public function display_pages($seo_name){
echo $seo_name;
}
}
i receive this message
Not Found
The requested URL /ci/pages/portofolio was not found on this server.
Try removing $1 at the end of your route. Make it look like this :
$route['pages/(:any)'] = 'display/display_pages';
Your route.php is correct. The way you are printing the $seo_name is not.
public function display_pages(){
echo $this->uri->segment(3); // it will print $1
}
Check http://ellislab.com/codeigniter/user-guide/libraries/uri.html
The htaccess should be something like
<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>

CodeIgniter not working with subfolders

I have my codeigniter code in web root. The mod_rewrite is enabled.. I checked through phpinfo.php. Now the code structure is something like this
controllers/home.php(default controller)
controllers/products.php (not listed in routes.php under config)
and then a subfolder
controllers/members/login.php
The urls I am trying are
domain_name/ ---------> works
(Note: this is where my echo base_url is pointing me to I guess
because $config['index.php'] = '', But even setting it to index.php is
not pointing me to the working index.php url)
domain_name/products ------> doesn't work
domain_name/index.php/products ------> works
similarly
domain_name/members ----->doesn't work
domain_name/index.php/members --->work
Because this thing is working with index.php I am guessing the routes.php is working fine. But some how echo $base_url is pointing me to these without index.php urls.
I have tried the .htacess file which is in my webroot that is /var/www/
The version for codeigniter is 2.1.3
Please help. I want this to work with or without index.php and if you can explain what i am missing please give me explaination.
In General Codeigniter tends to explicity hide index.php when call the its main root but when your trying to access its subfolders directly in the url .. index.php is required.In order to do that so you must include an .htaccess inside your ci filesystem that will accept the use of domain/product or domain/index.php/product
So try to include this .htacess and change its rewrite base into your ci folder name
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cifolder
#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>

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.

Resources