Remove index.php from url in CodeIgniter 3
Asked Answered
L

8

10

I am doing a project in CodeIgniter 3. I need to remove index.php from url. For that help me to get .htaccess file for CodeIgniter 3 and also where to place this file.

This is my baseurl

http://cableandmedia.com/demo/
Lantha answered 20/7, 2016 at 9:42 Comment(2)
possible duplicate of #1445885, you should search it before ask a questionMerimerida
Not only is this a duplicate question, but the answer is clearly spelled out in the CodeIgniter documentation if you'd only bother to look.Habile
D
23

Update your htaccess file with the below code

RewriteEngine On
RewriteBase /demo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

and in config file, please change base url with below code:-

$root  = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url']    = $root;
Disbelieve answered 20/7, 2016 at 9:52 Comment(0)
J
14

Place your .htacces file in the root directory and use the following code:

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?/$1 [L]
Jilolo answered 20/7, 2016 at 9:51 Comment(0)
A
14
  1. Create .htaccess file at root of the project

.htaccess

<IfModule mod_rewrite.c>
  RewriteEngine On
  # !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
  #  slashes.
  # If your page resides at
  #  http://www.example.com/mypage/test1
  # then use
  # RewriteBase /mypage/test1/
    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>

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin: "*"
</IfModule>

2. Remove index.php at the root/application/config/config.php

$config['index_page'] = '';

3. If your server is new | your server rewrite mode was denied than Open Terminal Connect Your Server Via SSH Type Below Code

sudo a2enmod rewrite
sudo service apache2 restart
sudo nano /etc/apache2/apache2.conf

After that please check AllowOverride Is All or not

<Directory “/var/www”>
  AllowOverride All
</Directory>
Alveta answered 20/7, 2018 at 9:30 Comment(0)
M
2

File: config.php

$config['index_page'] = '';

File: .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  

RewriteEngine allows you to rewrite URL requests that come into your server and is based on a regular-expression parser.

Musk answered 20/7, 2016 at 11:33 Comment(0)
O
1

In your config.php make following changes.

$config['index_page'] = '';

And add .htaccess file in main project directory with following content

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]  

<Files "index.php">
AcceptPathInfo On
</Files>
Ocieock answered 20/7, 2016 at 9:51 Comment(0)
P
1

Well,

Here what I did,

I edited/create ".htaccess" to this

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

and "app/config/config.php"

$config['base_url'] = '';
$config['uri_protocol'] = 'AUTO';

and "app/libraries/Template.php"

define("PATH", base_url()."index.php/");
define("BASE", base_url());

Cheers!

Poyang answered 19/1, 2018 at 22:19 Comment(0)
A
1
  1. Create a .htaccess file at the project's root and add the code below.
RewriteEngine on   
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
  1. In your config.php make the following changes.

$config['base_url']    = 'PUT YOUR BASE URL HERE';    
$config['index_page'] = '';
Acyclic answered 4/9, 2022 at 15:1 Comment(0)
C
0
 1. open your .htaccess file present in the root and add the below code :-
RewriteEngine On
RewriteBase /demo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]


2. Now in your config.php file 
 update this line of code :- $config['index_page'] = '';
Cretonne answered 16/2 at 5:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.