81 responses to “CodeIgniter htaccess”

  1. Kyle

    Really helpful, thanks.

  2. juandelacruz

    Nice. This is something to try on…

  3. Jeremy Blake

    Dude, most of your blog posts are way over my head. I need to read your blog more. I might learn something important! Keep it up.

  4. marcello

    hi, you forgot /index.php/welcome is the same as / , but your solution doesn’t redirect it.

    can you provide some solution for this to be complete?
    I tried RewriteRule index\.php/(.*)$ $1 [L,R=301] (doesn’t work well)

    it works with /index.php/welcome, but there’s an infinite loop in things that doesn’t work, so it’s definitely not a good solution. Thank you

  5. David

    If you want to force trailing slashes then you can use this rule instead.

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

  6. Jaap

    Hi, Thank you for this useful post.
    I implemented the .htacces file at one of my projects.

    But there is still one problem I think.
    It is still possible to request:

    Is there a way to remove the index.php?
    Thanks in advance!

  7. Jaap

    Sorry, I see the solution now, didnt read the comments 😉

  8. Chuck Lidderdale

    An annoying dupe problem. On a shared host my htaccess file contains
    RewriteCond %{HTTP_HOST} blackforest-co.com
    RewriteCond %{REQUEST_URI} !/blackforest/
    RewriteRule ^(.*)$ /blackforest/$1 [L]

    that works fine – except when one clicks on a link it comes back as:
    http://www.blackforest-co.com/blackforest/bfcc/ when it should be (which works)

    Just doesn’t look … clean. Any fix? Thanks

  9. Ra

    Really nice tutorial to keep in your bookmarks. Best about htaccess I have yet seen.

  10. Tzvook

    .htacsess us still voodo to me :-(
    I got panalized by google fot this one:
    Options +FollowSymLinks
    rewriteEngine on
    # Internally rewrite .my-domain.tv/ to my-domain.tv/pages//
    rewriteCond $1 !^pages/
    rewriteCond %{HTTP_HOST} !^www\.my-domain\.tv
    rewriteCond %{HTTP_HOST} ^([^.]+)\.my-domain\.tv
    rewriteRule (.*) /pages/%1/$1
    Do you see any reason for it in here ?

  11. Tzvook

    10X Dimas

  12. azifet

    Nice post there… however form data in the post variable $_POST gets lost whenever I redirect to the bootstrap file. any idea on how to work around this?

  13. alex

    tx for sharing this. I am using it for my first CI project which also holds a WordPress subfolder. So i can get some wp-posts in CI views.
    But the default page url doesn’t work mydomain.com/?p=123
    should i edit the .htaccess.

    any suggestions,
    ps i have wp in /blog and in the end i like to have something blog/%custom%

  14. alex

    Hi Dimas,

    I think i am going to have a problem with CI + WP and SEO url’s/permalink, see my topic on http://codeigniter.com/forums/viewthread/147068/

    How would you bypass /blog/ and have the url request go to CI or WP???
    Hope i am making sence


  15. pit

    it works perfectly … comments are simple but very helpful … great job in fact!


  16. Asif Iqbal


    Nice post.. I am new in the Codeigniter and this post really helpful for me.

    Thanks mate.

  17. zeratool

    You can simplify that to this:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [QSA,L]

  18. Julio Bitencourt

    Dude! This definitely saved my life! Tks a lot!

  19. Nuno

    Hi Dimas, thanks for the nicely documented .htaccess
    We can learn a lot from documented code lines…

    I’m setting up my first complex CI project. It will be hosted at my job (public health care) and it will powerup our intranet and webapps.

    And now my question.
    I set up a ci_sys, a ci_app, and a ci_www at the same level. Then I setup an alias pointing to the ci_www and making it my public folder.
    # ci_app
    # ci_sys
    # ci_www
    # # index.php
    # # ci_assets
    # # # css
    # # # js
    # # # img
    So I basically set config file system_application to `../ci_sys` and aplication to `../ci_app`

    With your .htaccess, I’m redirected one folder up in my alias
    ex: base_url set to
    is redirected to

    Can you point me somewhere? Thanks

  20. Mehedi

    I host my website nibssolutions.
    First time my site work well.
    But this time it show many error.
    Help me.

  21. sonnt

    Very good, Thanks U

  22. Eduardo

    Pessoal, criei um site no Codeigniter e agora quero criar um blog vinculado a ele, ou seja, para ficar assim: http://www.meusite.com.br/blog. Alguém pode me dizer como posso fazer isso?

  23. Laurence Dawson

    These are some really handy tips, thanks!

  24. samantha
  25. rubena

    can anyone convert this to htaccess ?

    $clean_url = str_replace(‘index.php’,”,current_url());
    $current_url = ‘http://’.$_SERVER[‘HTTP_HOST’].$_SERVER[‘REQUEST_URI’];


  26. Nayan

    Please write some posts about , how to setup Smarty 3 with CI ..

  27. Adamantus

    I didn’t really learn anything from this but wanted to. I would have liked it if you had explained the code more. For example what does:

    RewriteRule (welcome(/index)? mean? What does the ? mean here? Does the (/index) imply what?

  28. Tom

    I’m getting some crawl errors in Google which point to a page that does not exisit on my website:

    What Rewritecond (or Redirect) script can I add to my .htaccess to redirect this to my homepage?


  29. pit

    i’m not pro but it seems that something like this should work (untested) :
    RewriteRule ^\?p=[0-9]+$ / [L,R=301]
    reads :
    starting with
    the string “?p=” once
    followed with one or more Numeric character (between 0-9)
    that is last character
    then redirect 301 to home

    Not sure about the starting character, if it does not work, try
    RewriteRule ^p=[0-9]+$ / [L,R=301]
    RewriteRule ^.*\?p=[0-9]+$ / [L,R=301]

    hope it can help ^^

  30. Tom

    I tried each one but they all result in the message “Nothing found for ?p=168”
    Thanks for trying anyway


  31. pit

    finally after some “live” testing and googling for help here’s the code that works for me :)

    RewriteEngine on
    RewriteBase /
    RewriteCond %{QUERY_STRING} ^p=[0-9]+$
    RewriteRule ^(.*)$ ? [R=301,L]
    ##OR: RewriteRule ^(.*)$ index.php? [R=301,L]
    ##where you can replace index.php with the landing page you want

    Hope this time it’ll work as you need ^^

  32. Tom

    That’s great, you’ve cracked it! Thanks & well done


  33. Derk


    First i want to say that this is the best ever topic about CI url routes and mod_rewrite.

    i have a question for my urls, they are like:
    http://example.com/username wich is working

    now this isnt working (im useing “alternative to get” for ci)
    however this is working when:

    is there away to remove the /index/ from my “alternative to get” urls?
    I have it all working for my /index.php/ and controller/index
    its working like controller/ wich i find usefull!

  34. Dennis

    Your explanation of how rewrite works was great and I can successfully implement it. However I loose my style sheet. This is the error I get:
    [client] client denied by server configuration:

    Anyone have any clues??

  35. Francisco


    I’m using CI 2.0.2. Where the app folder lives outside the system folder.

    On my hosting setup I have a subdomain.domain.com pointing to my root folder where the CI lives.

    I would like to use the URL to match an application folder. So let me explain.

    I have the following folder set up:

    I would like to use: subdomain.domain.com/application1/controller/method

    And the .htaccess rules to rewrite it to:

    So then in the index.php I could get hold of the $_GET[‘app’] to match the application folder.

    Could you help me with that rewriting rule?

    Thank you

  36. Hank

    I’ve set up your re-write but my url gets written with a question mark added in certain conditions.

    If I go to:

    It gets re-written as

    I’m rubbish at reg expression and alike.

  37. Hank

    Thanks for the reply.

    I’ve settled with:
    RewriteRule ^(welcome(/index)?|index(\.php))/$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    The last ? has been removed from the first line.

    This works for:
    But not:
    http://domain.local/controller (note: the missing forward slash)


  38. Hank

    I’m using the re-write that removes trailing slashes also.

    When I made reference to it working with:
    1. http://domain.local/controller/
    but not:
    2. http://domain.local/controller

    What I mean’t was, it successfully removes “controller/” leaving:
    Whereas 2. does not change.

    What should be added to remove “controller”?
    Thanks H

  39. igi shellshock

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

  40. igi shellshock

    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'] = '';
    			$data['base'] = $this->config->item('base_url');
    	function logout()

    why still access index.php/dashboard/ ? not /dashboard/

  41. T. Brian Jones

    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.

  42. Johan van de Merwe

    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.

  43. Harry

    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.

  44. sh

    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

  45. Martin Hjort

    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” :)

  46. allis

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

  47. dude

    after pasting your .htaccess file, my default page goes to domain.ltd. even after i’ve deleted it already. how come?

  48. azwar akbar

    So complete codeigniter htaccess tutorial, awesome…

  49. Antonio

    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

  50. rot

    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

    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”

  51. sumit derbi

    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:
    alernatly i have used routes also
    same as
    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

  52. rani patil

    i got ‘404 Page Not Found’ error.plz help me .in .htaccess file .plz.
    Thanks .

  53. arion

    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 :)

  54. Rangan

    Good tutorial dude. Thanks

  55. Wes Dean

    This was very helpful. Thanks for posting it.

  56. jp

    I tried this but it keeps redirecting me to http://www.domain.tld/
    any help

  57. paras

    how to search index file function in route directory.
    please solve the problem and get answer please quickly.

  58. ridgerunner

    Your first “welcome” rule has a problem. It matches “welcomeindex” and “welcomeindex.php” (and not “welcome/index.php”

  59. Thomas

    How to remove the controller name from the url

  60. Sachin Sharma

    I have a HTML website with an issue:

    Google Webmaster Tool shows duplicate pages:


    I want only http://www.domain.com/folder/index.html to be visible and accessible. How can this be done with .htaccess ?

  61. rot


    Here’s a really usefull tip when you need to redirect old dynamic urls to new ones, specialy when there are GET vars in old urls (codeigniter refuse GET vars when using default config, and a simple Redirect 301 will fail)

    Here’s the solution :
    1. sample old url : page.php?id=1234&lang=en
    2. sample new url : newpage/action/1234#contact

    .htaccess :
    RewriteCond %{REQUEST_URI} ^/page\.php$ [NC]
    RewriteCond %{QUERY_STRING} id=([0-9]+)&lang=en$ [NC]
    RewriteRule ^(.*)$ en/newpage/action/%1#contact? [NE,R=301,L]

    Note that [NE] will allow to include an anchor in the destination url and the last caracter “?” will prevent query string to interfere with in the dest url.

    Hope it’ll save someone’s time !


    RewriteRule ^folder/?$ folder/index.html [R=301,L]

    should do the trick

  62. rot

    @sachin: you might replace [R=301,L] with [L]

  63. Mark LaDoux

    I’ve got an improvement, reflecting current Apache rules —


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

    to read as

    ### Remove index.php from URL
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
    RewriteRule ^ %1 [L,R=301]

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

    This way, the index.php gets removed whenever it’s included in the URL, your current rules only remove it when it’s mentioned after /start/ which should produce a 404 error, and therefore defeating the purpose.

    My method will remove it whenever it’s called after whatever RewriteBase is set to.

  64. ano

    By reversing the following line :
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]

    to something like :
    RewriteRule ^/?$ /index.php [L,R=301]

    Note: in “/index.php” you might remove the slash :unsure:

  65. hey


    Where should we put this .htaccess file(right next to index.php in the application folder ?)

    Same question for those using codeigniter HMVC.

    Thank you.