Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CI: Simple .htaccess using mod_rewrite for CodeIgniter
<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/
RewriteBase /
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>
@KungRaseri

This comment has been minimized.

Copy link

commented Jun 28, 2013

Worked perfectly :) Thanks man

@jfitzsimmons2

This comment has been minimized.

Copy link

commented Sep 9, 2013

Brilliant, thanks for sharing.

@panvagenas

This comment has been minimized.

Copy link

commented Oct 7, 2013

Thanks man!

@ghost

This comment has been minimized.

Copy link

commented Oct 9, 2013

Thanks!

@amirak17

This comment has been minimized.

Copy link

commented Oct 13, 2013

Thanks a lot Philip. God bless you.

@fadsel

This comment has been minimized.

Copy link

commented Feb 12, 2014

Brilliant :D

@haidarvm

This comment has been minimized.

Copy link

commented Feb 26, 2014

thanks ...

@Kerollmops

This comment has been minimized.

Copy link

commented Apr 18, 2014

Thanks man !

@rahulsompura

This comment has been minimized.

Copy link

commented May 15, 2014

Nice ... Man It Works....>>>>!@

@rahulsompura

This comment has been minimized.

Copy link

commented May 15, 2014

Nice ... Man It Works....>>>>!@

@rahulsompura

This comment has been minimized.

Copy link

commented May 15, 2014

Nice ... Man It Works....>>>>!@

@rahulsompura

This comment has been minimized.

Copy link

commented May 15, 2014

Nice ... Man It Works....>>>>!@

@rahulsompura

This comment has been minimized.

Copy link

commented May 15, 2014

Nice ... Man It Works....>>>>!@

@rahulsompura

This comment has been minimized.

Copy link

commented May 15, 2014

Nice ... Man It Works....>>>>!@

@trfiladelfo

This comment has been minimized.

Copy link

commented Jun 5, 2014

Thanks Man, it perfect works...

@HACK21

This comment has been minimized.

Copy link

commented Jul 1, 2014

Thx !

@pradeepnama2009

This comment has been minimized.

Copy link

commented Jul 5, 2014

Thanks :)

@rajakannan

This comment has been minimized.

Copy link

commented Jul 21, 2014

love it !

@HelgeSverre

This comment has been minimized.

Copy link

commented Jul 21, 2014

Thanks gives 1 free internet

@gnabino1

This comment has been minimized.

Copy link

commented Jul 23, 2014

it didnt work ..........why???????????????

@joshlminga

This comment has been minimized.

Copy link

commented Sep 4, 2014

It worked perfectly for CodeIgniter 2.2

@kmwalsh

This comment has been minimized.

Copy link

commented Sep 9, 2014

Thank you so much! I inherited a CodeIgniter project. I'd tried literally every other result for a fix (switching uri_protocol and index_page in config, a few different .htaccess files, etc.) and this worked on the first shot. Thanks again!

@chandu4ugandhi

This comment has been minimized.

Copy link

commented Jan 28, 2015

I am having a problem in my codeigniter site. Its not deleting or showing any nested files like this is url:

http://domain.com/project/admin/posts/delete/94

Its just showing the post index page only.

@xsolz-abhishek

This comment has been minimized.

Copy link

commented Feb 10, 2015

Why not the below code working as it is given to user guide??
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

@ton-ton

This comment has been minimized.

Copy link

commented Feb 26, 2015

Hey, how to modify this in order to work it well in GoDaddy? Thanks.

@virdisss

This comment has been minimized.

Copy link

commented Mar 15, 2015

Thanks, i appreciate!

@ztobs

This comment has been minimized.

Copy link

commented Apr 14, 2015

hello developers, im quite new to CI. Ive been developing a CI project for a while on my local machine and all has been working fine. Then i tried hosting it on the production server and all i get is

500 - Internal server error.
There is a problem with the resource you are looking for, and it cannot be displayed.

Has someone experienced this before. Your contribution would be highly appreciated.

@prashanthsandela

This comment has been minimized.

Copy link

commented May 14, 2015

Thanks.. 😄 This reduced lot of research. I appreciate it..

@maddbr

This comment has been minimized.

Copy link

commented May 16, 2015

Thanks again.

@usamamashkoor

This comment has been minimized.

Copy link

commented Jun 16, 2015

How can i replace search with pakistan in the given url
http://www.mysite.pk/jobs/search/faisalabad/all-cats

I want to change it to http://www.mysite.pk/jobs/pakistan/faisalabad/all-cats
How can i do this using .htaccess

@cstanda

This comment has been minimized.

Copy link

commented Jul 9, 2015

Great, Thanks!

@RGKrish183

This comment has been minimized.

Copy link

commented Jul 18, 2015

hey, i'm newbie for ci. i've try to remove index.php file,.. but i can't get it.

i've try the following .htaccess file

RewriteEngine On

RewriteBase /jtrack/

My sudir is jtrack - like localhost/jtrack/index.php/controller/function

but, I need to redir - like localhost/jtrack/controller/function

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 !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

And, in my config file like as

$config['base_url'] = '';

$config['index_page'] = 'index.php';

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

in my php was looks like,

$this->load->helper('url');

thanks for reading query...

@bionic2k

This comment has been minimized.

Copy link

commented Jul 27, 2015

Change the following Vars in your "application/config/config.php" File:

$config['base_url'] = 'http://domain.com/subfolder/';

This should never be empty. Include full URL to your Website´s DocRoot and don´t forget the Trailing Slash at the end.

$config['index_page'] = ' '; Remove index.php entry, now your .htaccess File makes routing to index.php

Hope it can help you ;)

The following entrys were not needed unless you want to add a System- or Application-Controller.
htaccess File prevent access to the system and application folder.

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

Here is the content of my .htaccess File for CI

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /subfolder/

# Canonicalize Codeigniter URLs

# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php|html?)?)/?$ / [R=301,L]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]

# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

# Enforce no-www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

# 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]

# 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>

# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php

</IfModule>

@olegzhermal

This comment has been minimized.

Copy link

commented Aug 11, 2015

Worked. Thanks. RewriteBase is what I was looking for =))

@teelake

This comment has been minimized.

Copy link

commented Sep 9, 2015

hi guys i am facing a similar error
my web app worked on my localhost but after uploading it on the webserver i cant access all pages except for the homepage(index page)
Each time i try to visit pages like about , contact i always get this error
ERROR 404 PAGE NOT FOUND
FILE NOT FOUND THIS SERVER
pls what is the way out. Thanks

@AyemyaThi

This comment has been minimized.

Copy link

commented Sep 22, 2015

I appreciated! It works. Thanks

@8ivek

This comment has been minimized.

Copy link

commented Nov 17, 2015

Thanks ^_^

@Jagarm

This comment has been minimized.

Copy link

commented Dec 8, 2015

Thanks a lot, I have been trying to get it working on my site and this worked, right out of the box

@ahsankhan609

This comment has been minimized.

Copy link

commented Jan 12, 2016

I have tried it a lot but it is not working for me. I have uploaded my Code Igniter Project on go Daddy... but still getting 404 Error Page...Please help me.

Even I have no sub folder. I am just uploading my project directly to public_html folder.

@denisdemais

This comment has been minimized.

Copy link

commented Mar 16, 2016

works fine!

@ron4stoppable

This comment has been minimized.

Copy link

commented Apr 21, 2016

thanks, this worked flawlessly

@jbmadking

This comment has been minimized.

Copy link

commented Apr 30, 2016

Worked perfectly. Thanks.

@lhesky

This comment has been minimized.

Copy link

commented May 6, 2016

hi! i am new to .htaccess anybody commments or recommendation on how do i start... im trying to my local host but not working so i try to join this group.tnx

@9-shen

This comment has been minimized.

Copy link

commented May 11, 2016

Thank u

@lanoux22

This comment has been minimized.

Copy link

commented Jun 16, 2016

and for escaping the wsdl from this rule how can i do? because after passing all my website in friendly url, the url of the web service given by NuSoap is unreachable

@seymer

This comment has been minimized.

Copy link

commented Jun 17, 2016

RewriteEngine On Options +FollowSymlinks # !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/ RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/home/?/$1 [L]

RewriteRule ^(.*)/welcome/$ $1/welcome/index [L]

@Lorena93

This comment has been minimized.

Copy link

commented Aug 12, 2016

My local project with Xampp 1.8.1 and codeigniter 3.0.6 is not running.
File htaccess:

`
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/

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

<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

`
File config:
$config['base_url'] = 'http://localhost/codeigniter';
$config['index_page'] = '';

$active_group = 'default';
$query_builder = TRUE;

File database:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'sistemaderiego',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

File route:
$route['default_controller'] = 'Prueba';

any Help?

@sawankjava

This comment has been minimized.

Copy link

commented Aug 30, 2016

You are great!!!!!!!!!

@vrkansagara

This comment has been minimized.

Copy link

commented Sep 5, 2016

I were proposed a best solution for any project here.

@gbrock

This comment has been minimized.

Copy link

commented Sep 16, 2016

@vrkansagara thanks for sharing, yours worked for me when the above did not.

@geovanent

This comment has been minimized.

Copy link

commented Oct 5, 2016

Muito Obrigado.
Brasil Representando...

@ntaloventi

This comment has been minimized.

Copy link

commented Nov 28, 2016

works perfectly on shared hosting

@sumitnair26

This comment has been minimized.

Copy link

commented Dec 9, 2016

after writing as RewriteRule ^(.*)$ index.php?/$1 [L] i am not able to enter base url $config['base_url'] = '/test/'; please help me out what changes should i do

@bill-goldberg

This comment has been minimized.

Copy link

commented Jan 22, 2017

I am just started to use codeigniter ...can anybody help me... i am working on a project developed using codeigniter and the project has a 'public' directory... how can i modify the .htaccess file so that i don't have to mention public directory in the link , e.g www.example.com/public

@sourabhkshinde

This comment has been minimized.

Copy link

commented Jan 31, 2017

thanks you so much..

@calberto185

This comment has been minimized.

Copy link

commented Feb 21, 2017

Hello one question!!!.
I am working with a template which is out system and application, and my login driver is login, where I load the view and also a method to validate login data. My question is why when I log in I do not redirect to the url with the controller and the method I already mentioned "velify". I was testing with the simplification of index.php in the .htaccess which if it works but when I put it does not testringa the folder where the template is housed it removes the styles of the view and in this case if I process the data when I log , But as you know this restricts me the template folder.
Note: for the login form I am using the codeigniter helper. This is my code view login:

<?= form_open('main/identify') ?> <div class="input-group"> <span class="input-group-addon" id="basic-addon1"> <i class="fa fa-user" aria-hidden="true"></i></span> <?= form_input($user) ?> </div> <div class="input-group"> <span class="input-group-addon" id="basic-addon2"> <i class="fa fa-key" aria-hidden="true"></i></span> <?= form_password($pass) ?> </div> <div class="text-center"> <?= form_input($submit) ?> </div> <?= form_close() ?>

This the code of the controller:

public function index(){
$this->load->view('main');
}
public function identify(){
$user = $this->input->post('username');
$pass = $this->input->post('password');
$result = $this->servicefire->Auth($user, $pass);
if ($result != null) {
header('location:'.base_url('admin/index'));
}else{
header('location:'.base_url());
}
}

This is my .htaccess:

RewriteEngine on
RewriteCond $1 !^(index.php|robots.txt|sitemap.xml|template)
RewriteRule ^(.*)$ index.php/$1 [L]

@arifurrahmanbd

This comment has been minimized.

Copy link

commented Mar 3, 2017

man it works

@anish137i

This comment has been minimized.

Copy link

commented May 6, 2017

Worked as expected for Godaddy

@felipsmartins

This comment has been minimized.

Copy link

commented Aug 31, 2017

it works!

@geronimo794

This comment has been minimized.

Copy link

commented Sep 2, 2017

Simple and work well

@gandernirbhay

This comment has been minimized.

Copy link

commented Nov 1, 2017

it now work on window server please help.

@faizalfian

This comment has been minimized.

Copy link

commented Dec 28, 2017

Great ! Works perfect.

@Baskargit

This comment has been minimized.

Copy link

commented Dec 30, 2017

Great ! Works Well

@pba247

This comment has been minimized.

Copy link

commented Feb 25, 2018

That works!

@guevaraj31

This comment has been minimized.

Copy link

commented Mar 1, 2018

thanks man!

@xemuj

This comment has been minimized.

Copy link

commented Apr 2, 2018

Great ! Works Well

@kingblizzy

This comment has been minimized.

Copy link

commented Apr 14, 2018

HI am Most greatful This Helped me out..... You are a good share mate.... Thanks.

@ghost

This comment has been minimized.

Copy link

commented May 19, 2018

Thanks dude! this helps a lot.. it was 100% working for linux mint

@xrb21

This comment has been minimized.

Copy link

commented May 30, 2018

thanks, it's work perfectly

@hikmetkutuk

This comment has been minimized.

Copy link

commented Jun 24, 2018

thanks, works great

@dhmskrn

This comment has been minimized.

Copy link

commented Sep 18, 2018

thanks, it works!

@lorenzochaudessolle

This comment has been minimized.

Copy link

commented Oct 2, 2018

fck u its fcking bad ! it doesnt work ! big scum

@rajiv2815

This comment has been minimized.

Copy link

commented Feb 18, 2019

thanks a ton brother ... helps me a lot in CI... was dead before to see that...tx again

@hith97

This comment has been minimized.

Copy link

commented Mar 20, 2019

Hello My site Can't reach i try all solution it is an immediately please Help .

It is only load nothing to see anything .

@AlbertBeweb

This comment has been minimized.

Copy link

commented Apr 8, 2019

Thanks a lot ;)

@fahadjunaid60

This comment has been minimized.

Copy link

commented Aug 3, 2019

thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.