Removing "index.php" with CodeIgniter

So, you've tried to remove the index.php file from your CodeIgniter URLs and it's not working properly. What to do?

Make sure you have followed the instructions in the CodeIgniter manual. If you have done that, and are still having problems, then I have an .htaccess file and a few other tips that will get you up and running.

The following code segment is an .htaccess file and is well commented so that you can customize it to your needs.

Use the following for your .htaccess, making changes where noted:

  1. #-------------------------------------------------------------------------------
  2. # CodeIgniter .htaccess file for the main index.php directory
  3. #-------------------------------------------------------------------------------
  4.  
  5. #-------------------------------------------------------------------------------
  6. # If you must have your CI "system" directory at http://www.domain.tld/system/,
  7. # Then I advise you to place a .htaccess file in that directory
  8. # with "deny from all" in it.
  9. #-------------------------------------------------------------------------------
  10.  
  11. #-------------------------------------------------------------------------------
  12. # Be sure to replace "domain.tld" with your actual domain and tld anywhere this
  13. # appears in this file.
  14. #-------------------------------------------------------------------------------
  15.  
  16. #-------------------------------------------------------------------------------
  17. # Restore "allow from all" / I use a "deny from all" in some directory
  18. # structures, especially where I am hosting multiple sub-domains that point to
  19. # subdirectories of the main domain, this restores it.
  20. #-------------------------------------------------------------------------------
  21. #allow from all
  22.  
  23. #-------------------------------------------------------------------------------
  24. # Turn off directory listings for increased security
  25. #-------------------------------------------------------------------------------
  26. Options -Indexes
  27.  
  28. #-------------------------------------------------------------------------------
  29. # Turn on following symbolic links.
  30. #-------------------------------------------------------------------------------
  31. Options +FollowSymLinks
  32.  
  33. #-------------------------------------------------------------------------------
  34. # Some web servers require this for index.php to be removed properly. You may
  35. # try your .htaccess with or without the following line.
  36. # Most work fine with it.
  37. #-------------------------------------------------------------------------------
  38. Options -MultiViews
  39.  
  40. #-------------------------------------------------------------------------------
  41. # Make index.php the directory index page
  42. #-------------------------------------------------------------------------------
  43. DirectoryIndex index.php
  44.  
  45. #-------------------------------------------------------------------------------
  46. # Always make sure mod_rewrite is installed before using.
  47. # If you are on a Linux server, uncomment the line with mod_rewrite.c
  48. # If on a Windows server using XAMPP, uncomment the line with mod_rewrite.so
  49. #-------------------------------------------------------------------------------
  50. <IfModule mod_rewrite.c>
  51. #<IfModule mod_rewrite.so>
  52.  
  53. #-----------------------------------------------------------------------------
  54. # Turn on the RewriteEngine
  55. #-----------------------------------------------------------------------------
  56. RewriteEngine on
  57.  
  58. #-----------------------------------------------------------------------------
  59. # Set the base directory where the application root is located.
  60. #-----------------------------------------------------------------------------
  61. # If your application is at http://www.domain.tld/path/to/CI_Application/
  62. # then uncomment the following line. Change /path/to/CI_Application/ to the
  63. # actual URL path not including the domain.tld (or localhost).
  64. # This includes using a subdirectory on localhost, like
  65. # http://localhost/path/to/CI_Application/
  66. #RewriteBase /path/to/CI_Application/
  67.  
  68. # If your application is at http://www.domain.tld/ (or is http://localhost/)
  69. # then uncomment the following line.
  70. RewriteBase /
  71.  
  72. #-----------------------------------------------------------------------------
  73. # The following 2 lines will force a www.prefix. If you want to make sure
  74. # your site always displays the www subdomain, then uncomment the following
  75. # 2 lines.
  76. #-----------------------------------------------------------------------------
  77. #RewriteCond %{HTTP_HOST} !^www\.domain\.tld$ [NC]
  78. #RewriteRule ^(.*)$ http://www.domain.tld/$1 [R=301,L]
  79.  
  80. #-----------------------------------------------------------------------------
  81. # The following 2 lines will remove a www prefix. If you wish to remove the
  82. # www and always have http://domain.tld instead of http://www.domain.tld
  83. # then uncomment the following 2 lines.
  84. #-----------------------------------------------------------------------------
  85. #RewriteCond %{HTTP_HOST} ^www\.domain\.tld$ [NC]
  86. #RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]
  87.  
  88. #-----------------------------------------------------------------------------
  89. # If a controler can't be found - then issue a 404 error from PHP
  90. # Error messages (via the "error" plugin)
  91. #-----------------------------------------------------------------------------
  92. #ErrorDocument 403 /index.php/403/
  93. #ErrorDocument 404 /index.php/404/
  94. #ErrorDocument 500 /index.php/500/
  95.  
  96. #-----------------------------------------------------------------------------
  97. # Deny any people (or bots) from the following sites: (to stop spam comments)
  98. #-----------------------------------------------------------------------------
  99. #RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
  100. #RewriteCond %{HTTP_REFERER} porn\.com
  101. #RewriteRule .* - [F]
  102. # NOTE: If you are having trouble from a certain URL just
  103. # add it above to forbid all visitors from that site.
  104.  
  105. #-----------------------------------------------------------------------------
  106. # You can also uncomment this if you know the IP:
  107. # Just place the IP address you wish to deny here.
  108. # You may use multiple IP addresses separated by a space.
  109. # You may also use a domain, tld, or a part thereof.
  110. # Deny from 192.168.1.1 127.0.0.1
  111. # Deny from .net example.com
  112. # The above will deny from example.com and from all .net locations.
  113. #-----------------------------------------------------------------------------
  114. #Deny from 192.168.1.1
  115.  
  116. #-----------------------------------------------------------------------------
  117. # METHOD #1 of HIDING PHP FILES. (There is an alternate method further down.)
  118. # If the file is NOT the index.php file then
  119. # Hide all PHP files so none can be accessed by HTTP.
  120. # This may not be desirable in all cases.
  121. #-----------------------------------------------------------------------------
  122. #RewriteCond %{REQUEST_FILENAME} !index.php
  123. #RewriteRule (.*)\.php$ index.php/$1
  124.  
  125. #-----------------------------------------------------------------------------
  126. # The following lines begin the conditional setup for removing index.php
  127. # Use either METHOD #1 or METHOD #2, but not both.
  128. #-----------------------------------------------------------------------------
  129. # METHOD #1
  130. # If you want to redirect all files and directories that do not exist (404)
  131. # then use the following 2 statements. This is how
  132. # http://domain.tld/controller/ is redirected to http://domain.tld/index.php
  133. # because /controller/ doesn't really exist, so now CI can use the URL to
  134. # call the correct controller. This is the standard and default method.
  135. RewriteCond %{REQUEST_FILENAME} !-f
  136. RewriteCond %{REQUEST_FILENAME} !-d
  137.  
  138. # METHOD #2
  139. # If you want to redirect all files/directories that are not a request
  140. # for the following:
  141. # index.php, robots.txt, favicon.ico, /public/, /img/, /css/, /js/
  142. # then use the following statement.
  143. # If you have other applications that are non-CI integrated into your site,
  144. # Like a third-party forum or blog, then this will disable it. Do not
  145. # use if you need to call third-party PHP files.
  146. #RewriteCond $1 !^(index\.php|public|img|css|js|robots\.txt|favicon\.ico)
  147.  
  148. # ADDITIONAL REWRITE CONDITION: This may be used in addition to one of the
  149. # above METHODs.
  150. # METHOD #2 of HIDING PHP FILES: This is to be used exclusively of the
  151. # method #1 of hiding PHP files listed above. Do not use both methods.
  152. # Explanation: If you would like to hide all PHP files so none can be
  153. # accessed by HTTP, then use the following line.
  154. # NOTE: If you have other applications that are non-CI integrated into your
  155. # site, like a third-party forum or blog, then this will disable it. Do not
  156. # use if you need to call third-party PHP files.
  157. # If you need heightened security against use of PHP files, see my tutorial
  158. # on how to properly protect your directories.
  159. # NOTE: I have not personally tested this condition yet. If you have tested
  160. # it and verified it works or does not work, please contact me and tell me
  161. # about it. http://www.danielwmoore.com/contact.
  162. #RewriteCond %{REQUEST_FILENAME}\.php -f [NC]
  163.  
  164. #-----------------------------------------------------------------------------
  165. # use the following line if CI is in the root of the URL.
  166. # You may need to use index.php/$1 or index.php?/$1
  167. # Try it with or without the ? after index.php to see which works for you.
  168. # GoDaddy accounts require the ?
  169. # My localhost with XAMPP on Windows also requires the ?
  170. # Most hosts work correctly without the ?, so try it without first unless
  171. # you know your host requires it. It is placed here so you will know where
  172. # it goes in case you need it.
  173. # [NC] = no case - case insensitive
  174. # [L] = Last rule, last rewrite for this set of conditions
  175. # [QSA] = Query String Append, should be used to prevent all redirects from
  176. # going to your default controller, which happens on some server
  177. # configurations.
  178. #-----------------------------------------------------------------------------
  179. #RewriteRule ^(.*)$ index.php?/$1 [NC,L,QSA]
  180. RewriteRule ^(.*)$ index.php/$1 [NC,L,QSA]
  181.  
  182. #-----------------------------------------------------------------------------
  183. # use the following if CI is in a sub-directory, and place this
  184. # file IN that subdirectory.
  185. # If your CI Application is at http://www.domain.tld/CI_Application/
  186. # Replace 'CI_Application' with the name of the sub-directory.
  187. # Try it with or without the ? after index.php to see which works for you.
  188. # See above for the reasons for the '?'.
  189. # Be sure that this matches the RewriteBase above.
  190. # [NC] = no case - case insensitive
  191. # [L] = Last rule, last rewrite for this set of conditions
  192. # [QSA] = Query String Append, should be used to prevent all redirects from
  193. # going to your default controller, which happens on some server
  194. # configurations.
  195. #-----------------------------------------------------------------------------
  196. #RewriteRule ^(.*)$ CI_Application/index.php?/$1 [NC,L,QSA]
  197. #RewriteRule ^(.*)$ CI_Application/index.php/$1 [NC,L,QSA]
  198.  
  199. #-------------------------------------------------------------------------------
  200. # Don't forget to end the IfModule check for the rewrite engine.
  201. #-------------------------------------------------------------------------------
  202. </IfModule>
  203.  
  204. #-------------------------------------------------------------------------------
  205. # If Mod_rewrite is NOT installed go to index.php
  206. # Remember to use mod_rewrite.c for Linux and mod_rewrite.so for Windows.
  207. #-------------------------------------------------------------------------------
  208. <IfModule !mod_rewrite.c>
  209. #<IfModule !mod_rewrite.so>
  210. ErrorDocument 404 index.php
  211. </IfModule>

Open up your application/config.php and find the line that contains "$config['uri_protocol'] =". Check the value.

Try each of the values listed in the comment above it one at a time ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') and see if that fixes the problem. Start with 'AUTO'.

Find the following line in your config.php file:

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

And change it to the following:

$config[index_page'] = "";