
This is the CI .htaccess file that I use for my CodeIgniter PHP projects (some of these CodeIgniter htaccess rules can be used for any project, keep reading). CodeIgniter uses mod_rewrite to route all requests to the CI front controller. I’ve made some additions in an attempt to canocalize some of the CodeIgniter URL end points. The key benefit of a canocalized URL is that your search engine page ranking (page juice) is not spread across several pages, but instead, targeted to a single page.
htaccess Rule Breakdown
In your CodeIgniter project you will typically have a default controller and you will be able to access this controller at the following URLs:
/ /welcome /welcome/ /welcome/index /welcome/index/ /index.php
That’s a total of 6 URL end points pointing to the front controller. Additionally other controllers you’ve created will also have a default “index” method, consider the following:
/category /category/index /category/index/
Again, a total of 3 URL end points where there should only be one. The first set of rules in the .htaccess file prevent this:
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
With these rules the front controller will always be accessed at “/” and any other URLs pointing to a controller’s “index” method will be accessed at “/controller”. Notice that I also use a 301 redirect, this aids in maintaining or passing any existing search engine ranking (page juice) to the final redirected page.
So these:
/welcome /welcome/ /welcome/index /welcome/index/ /index.php
… would simple become:
/
… and:
/category /category/index /category/index/
… would become:
/category
Another important thing to note: for controllers with an “index” method which take any sort of parameter, these URLs will still work as such:
/welcome/index/123 /category/index/123
To be able to remove the “index” (or “welcome/index”) and still have parameters passed in, you will need to configure your routes file.
The next set of rules allow you to enforce “www” or enforce NO “www”, you will have to pick which you prefer and enable one or the other. Additionally, for those who may have subdomains, adding the subdomains to the list (if enforcing www) will safely exclude those subdomains from being redirected.
# 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.tld/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
The Full CodeIgniter htaccess File
Here is the entire .htaccess file that I use for my CodeIgniter projects. Remember that some of these rules are commented out, you will have to explicitly enabled them by removing the comments (removing the “#” from in front of the rule).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# 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.tld/$1 [L,R=301]
# Enforce NO www
#RewriteCond %{HTTP_HOST} ^www [NC]
#RewriteRule ^(.*)$ http://domain.tld/$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>
Apply These Final Touches to Clean Up the URL
Remember, once you have your CodeIgniter htaccess file setup, you will want to go into your “/system/application/config/config.php”, find the following:
$config['index_page'] = "index.php";
and change it to:
$config['index_page'] = "";
Because you are using Apache mod_rewrite, the above will set your config file so that “index.php” will NOT show up as part of the URL as such:
http://www.example.com/index.php/controller/method/variable
instead your URLs will be cleaner:
http://www.example.com/controller/method/variable
If you have comments or improvement suggestions please let me know.

i follow this tutor, my default controller is ‘dashboard’, it works.people can check here http://jobcareer.polibatam.ac.id but when i tried do like http://jobcareer.polibatam.ac.id/login didn’t work.please help me..thx..
Your problem is a little strange as if you default controller is “dashboard” the following should return you to “/” … http://jobcareer.polibatam.ac.id/dashboard … you should confirm that you have codeigniter configured correctly and that your htaccess rewrites are actually working, per the tutorial above, see if some of the URLs are indeed being redirected.
thx dimas for reply, this my .htaccess file
RewriteRule ^(dashboard(/index)?|index(\.php)?)/?$ / [L,R=301] RewriteRule ^(.*)/index/?$ $1 [L,R=301] RewriteCond %{HTTP_HOST} !^(www|jobcareer) [NC] RewriteRule ^(.*)$ http://www.polibatam.ac.id/$1 [R=301,L]config.php file
$config['base_url'] = 'http://jobcareer.polibatam.ac.id/'; $config['index_page'] = ''; $session->userdata('sesblabla')=="") { redirect('/login/'); }else{ $data['base'] = $this->config->item('base_url'); $this->load->view('dashboard',$data); } } function logout() { $this->session->sess_destroy(); redirect('/login/'); } }why still access index.php/dashboard/ ? not /dashboard/
Take a look at this, might solve your problem …
This is a great post. Thanks for taking care of all these details. These are often overlooked and can make a HUGE difference for SEO. I’ve seen sites of mine double their number of indexed pages, almost overnight, after making these improvements. It’s nice to have them done on my new project right from the start.
This only works if you have in your config.php file:
$config['uri_protocol'] = ‘AUTO’;
Which I hadn’t in first instance (QUERY_STRING).
Now it works fine.
All forms on my site now fail to post with the addition of the htaccess code. Could it be something to do with WAMPserver? I have followed the instructions exactly.
@Harry, if you have firebug, you will want to make sure that your forms POST/GET are not being redirected, else it may be loosing the data in between.
good art
@Johan van de Merwe +1
I had problems with my ajax get requests, had to change them to post and now works fine
You still have to do something in your controllers to prevent access to more than one URL for the same content. I tried using your rewrite_rules, while creating a simple blog controller
I can access the controllers comments-function through both http://www.fmground.dk/index.php/blog/comments and http://www.fmground.dk/blog/comments
which leaves an “open SEO wound”
help me pls..
i have virtual host like this :
domain => folder in server
hotels.visitbali.com => www/hotels
hotels.visitbali.com/kutahotel => www/kutahotel
if i access hotel.visitbali.com/kutahotel, there no problem.
but if i access hotel.visitbali.com/kutahotel/customer/booking, no page found.
customer is controller and booking is function
can someone help how about .htaccess ??
after pasting your .htaccess file, my default page goes to domain.ltd. even after i’ve deleted it already. how come?
@dude, browsers will sometime cache the request, so you might just need to restart your browser (chrome will do this sometimes)
So complete codeigniter htaccess tutorial, awesome…
Hi, Dimas. I’m a noob at this, and I crashed everything.
I pasted the file u posted into my htaccess file, and now, everytime I try to access my http://localhost/testweb, I get redirected into domain.tld. How can I fix it? Thank you
Did you copied and pasted without reading ?
it’s never a good idea.
to solve your problem, comment/remove the “enforce www” part and it should work better.
Plus search and replace the references to domain.tld with something approching your own project url
http://localhost/testweb
to avoid other bad behaviors, i would suggest you to look at using apache virtualhost as it is a good practise. search the web for “Apache Virtual Host”
I have read this. thanks for tut. i was searching for this . i have intranet with server name unicus-billion.And my CI path for my page is:
unicus-billion/resume/index.php/resumecontroller/load
alernatly i have used routes also
unicus-billion/resume/index.php/resumecontroller/load
same as
unicus-billion/resume/index.php/user.
after reading i have pasted & changed it. i got following error:-
———
Not Found
The requested URL /resume/resumecontroller/load was not found on this server.
Apache/2.2.6 (Win32) mod_ssl/2.2.6 OpenSSL/0.9.8g PHP/5.2.5 Server at unicus-billion Port 80
——–
will you please help me
to work my code simply without index.php in url.
thanks and regards
Sumit derbi
i got ’404 Page Not Found’ error.plz help me .in .htaccess file .plz.
Thanks .
hi Dimas,
i follow your totorial,
i think i have same problem with @allis
i have virtual host like :
b3.menlh.go.id in directory : home/b3/www/MENLH
this website is like the parent portal cms base on joomla
then i want to put CI application on the same server ,
so CI application extracted on home/b3/www/MENLH/registrasi directory
here is my configuration :
$config['base_url']= ‘http://b3.menlh.go.id/registrasi’;
$config['index_page'] = ”;
$config['uri_protocol'] =’AUTO’;
my default controller is “main”,
works fine when acces http://b3.menlh.go.id/registrasi
but i get error : “The requested URL /registrasi/main/login was not found on this server. ”
when acces http://b3.menlh.go.id/registrasi/main/login
plis help me dimas
thx so much
Good tutorial dude. Thanks
This was very helpful. Thanks for posting it.
hi.
I tried this but it keeps redirecting me to http://www.domain.tld/
any help
thanks
how to search index file function in route directory.
please solve the problem and get answer please quickly.
Your first “welcome” rule has a problem. It matches “welcomeindex” and “welcomeindex.php” (and not “welcome/index.php”
How to remove the controller name from the url