CodeIgniter htaccess problems
One of the most attractive features of CodeIgniter is the clean url structure that can be generated with such ease. The problem is getting the .htaccess rewrite to work on the first attempt. The first thing most users search for after installing CodeIgniter is a way to remove the index.php file from the url structure. I thought I would take care of that question here.
The CodeIgniter User Guide gives this example:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
But this has a tendency to fail for most people, who must then resort to the forums.
A more elaborate solution can be found in the CodeIgniter Forums.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#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>
# 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>
AddHandler php5-script .php
This should take care of your problem. If not shoot me a comment and I’ll see what I can do to help.
Tagged as CodeIgniter, htaccess + Categorized as CodeIgniter
7 Comments
Trackbacks & Pingbacks
-
URLs no CodeIgniter | CodeIgniter Brasil
[...] Em função de configurações de servidor, algumas vezes o .htaccess mostrado pode não funcionar. Caso isso aconteça com você, dê uma olhada nesse artigo sobre problemas de htaccess no CodeIgniter. [...]
I actually think the way CodeIgniter relies on PATH_INFO and REQUEST_URI for clean URIs is one of its weaknesses. PATH_INFO is stripped of important characters (no spaces) by the server, and REQUEST_URI (and how CI parses it) rules out the chance to use query string values when necessary. I wish it defaulted to a single query string trigger, like ?q=controller/function/param1/param2.
Some people (like me) have had to add QSA to the last line.
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
@Colin I agree that flexibility is always preferred, but that’s one of the great things about the CI framework. I personally prefer the absence of query strings (makes for cleaner urls), but thought I’d point you in the right direction. This thread in the CI Forums should get you on the right path: http://codeigniter.com/forums/viewthread/47107/
If you can ignore the back and forth bickering, it should get you what you need.
I haven’t any personal problems with setting htaccess on my site (I feel like I’m in the minority of people who actually took time to, you know, learn about my web server) or with using $_GET when it made sense to. But the limitations of PATH_INFO and REQUEST_URI exist regardless.
In user interface design we stress having good defaults to lessen the chance of user error. API design is no different.
thanks, useful
i still have index.php appearing in the url.