Samir Joglekar
Samir Joglekar

Reputation: 155

Unable to route to static PHP page in 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.

Upvotes: 0

Views: 1058

Answers (1)

Tweetie Sabado
Tweetie Sabado

Reputation: 137

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> 

Upvotes: 1

Related Questions