How to Remove Index.Php in the URL of Codeigniter Application
Submitted by nurhodelta_17 on Thursday, February 8, 2018 - 20:45.
Installing CodeIgniter
If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this source code. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_htaccess so I'm using the below code.- localhost/codeigniter_htaccess/
Enabling mod_rewrite in our Apache
Next, we need to configure our apache to enable mod_rewrite so that we can create our .htaccess file to rewrite our URLs. 1. Open httpd.conf in a text editor located in xampp/apache/conf folder where xampp is the location of your XAMPP application. 2. Find and remove # in the ff line:#LoadModule rewrite_module modules/mod_rewrite.so
3. Find all occurrences of AllowOverride and change the value if it is None to All. Make sure that all the values are change to All.
4. Save changes and restart XAMPP.
Creating our .htaccess File
In the root directory of your CodeIgniter application, which in my case codeigniter_htaccess, create a file and name it as .htaccess and paste the below code.- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule ^(.*)$ index.php/$1 [L]
- </IfModule>
Initiating our Test Example
To test this changes, we are going to create a simple routing application to test our URL.Configuring our App
First, we are going to configure our app by setting our base_url and removing index.php in our index_page. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find the ff line $config['base_url']. 3. Add the path of your app, in my case http://localhost/codeigniter_htaccess/. 4. Find the line $config['index_page'] and remove the index.php value.Creating our Default Controller
Next, we are going to create the default controller of our app. Means that if there no value in our URL, it will automatically go to our defined controller. Open routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.- $route['default_controller'] = 'routes';
Creating our Controller
Next step is to create our controller. Create a file named Routes.php in application/controllers folder of our app and put the ff codes.- <?php
- class Routes extends CI_Controller {
- function __construct(){
- parent::__construct();
- $this->load->helper('url');
- //$this->load->model('users_model');
- }
- public function index(){
- $this->load->view('home');
- }
- public function about(){
- $this->load->view('about');
- }
- public function blog(){
- $this->load->view('blog');
- }
- }
Creating our Views
Lastly, we create the views of our app. Take note that I've use Bootstrap in the views. You may download bootstrap using this link. Create the ff files inside application/views folder. home.php- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>CodeIgniter htaccess And Routing</title>
- <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="https://www.sourcecodester.com">SourceCodester</a>
- </div>
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- <li class="active"><a href="<?php echo base_url(); ?>routes/index">Home</a></li>
- <li><a href="<?php echo base_url(); ?>routes/about">About</a></li>
- <li><a href="<?php echo base_url(); ?>routes/blog">Blog</a></li>
- </ul>
- </div>
- </div>
- </nav>
- <div class="container">
- <div class="jumbotron">
- <h1 class="text-center">This is the <b>Home</b> page</h1>
- </div>
- </div>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>CodeIgniter htaccess And Routing</title>
- <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="https://www.sourcecodester.com">SourceCodester</a>
- </div>
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- <li><a href="<?php echo base_url(); ?>routes/index">Home</a></li>
- <li class="active"><a href="<?php echo base_url(); ?>routes/about">About</a></li>
- <li><a href="<?php echo base_url(); ?>routes/blog">Blog</a></li>
- </ul>
- </div>
- </div>
- </nav>
- <div class="container">
- <div class="jumbotron">
- <h1 class="text-center">This is the <b>About</b> page</h1>
- </div>
- </div>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>CodeIgniter htaccess And Routing</title>
- <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="https://www.sourcecodester.com">SourceCodester</a>
- </div>
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- <li><a href="<?php echo base_url(); ?>routes/index">Home</a></li>
- <li><a href="<?php echo base_url(); ?>routes/about">About</a></li>
- <li class="active"><a href="<?php echo base_url(); ?>routes/blog">Blog</a></li>
- </ul>
- </div>
- </div>
- </nav>
- <div class="container">
- <div class="jumbotron">
- <h1 class="text-center">This is the <b>Blog</b> page</h1>
- </div>
- </div>
- </body>
- </html>
Add new comment
- 942 views