Fixing CodeIgniter and .htaccess issues on Leopard

Hello

This is my first post and I’ll try to show you guys a few configurations regarding CodeIgniter with Leopard. First I was having problems with .htaccess and mode_rewrite, to remove de “index.php”. Searching CodeIgniter forums, many many people had same problem, 500 Internal Erros, 403 Forbidden and so on. I was almost givin up, when I tried working with Virtual Hosts on apache, witch is the best and the correct solution in my case, because I develop many sites. Well, just for the record, I’m not using pack solutions, like apachefriends.org, just de built-in apache+php5, and installed mysql.

I’ll show you how i solved my problem using Virtual Hosts. You have to edit the following files:

/etc/hosts
/private/etc/apache2/httpd.conf
/private/etc/apache2/extra/httpd-vhosts.conf

I used textmate, using terminal, type sudo mate . If you dont have textmate, use sudo open . It will open the default texteditor of MacOSx. Put your password and here we go:

hosts file:
[cc lang=”xml”]
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
127.0.0.1 testsite1.dev # Site no.1
127.0.0.1 testsite2.dev # Site no.2.
127.0.0.1 testsitex.dev # Here you can add as many lines (sites) you develop.
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
[/cc]

You don’t have to reboot the system or apache. In this example, if you type in safari “testsite1.dev”, you will be forward to the site configured in Virtual Hosts, even if “testsite1.dev” exists on internet.

httpd file:
this is a long file, so I’ll show a few parts. On my config, I commented the following line, I don’t have many users on my macbook, I only have my own user, so I don’t need this module to be loaded:

[cc lang=”xml”]
#LoadModule userdir_module libexec/apache2/mod_userdir.so
[/cc]

This is very very important to be sure these line are uncommented:

[cc lang=”xml”]
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
[/cc]

and

[cc lang=”xml”]
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
[/cc]

I don’t think you need to edit any thing more on this file. The last and more importante file is where you will setup avery website you develop.

httpd-vhosts file:

Do not touch this line:

[cc lang=”xml”]
NameVirtualHost *:80
[/cc]

Here is the root of your sites

[cc lang=”xml”]

DocumentRoot “/Users//Sites”
ServerName localhost

[/cc]

And here your Virtual Hosts:

[cc lang=”xml”]

DocumentRoot “/Users//Sites/
ServerName testsite1.dev
/Sites/“>
AllowOverride All
Options +FollowSymLinks
Order allow,deny
Allow from All


[/cc]

The following lines allow me to use the functions of .htaccess to remove the index.php, and fix the 403 forbidden problems.

[cc lang=”xml”]
AllowOverride All
Options +FollowSymLinks
[/cc]

When I was not using virtual hosts, I configured the single webserver on apache, and used something like that:

http://127.0.0.1/website1/webroot/
http://127.0.0.1/website2/webroot/
http://127.0.0.1/website-etc/webroot/

I had 500 Internal Server problems, and I’m almost sure it’s because the “root” of my “website1″ not being the actual root of my webserver.

Here is my .htaccess config:

[cc lang=”xml”]
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
[/cc]

So, I hope a could help anyone having the same problems I had. This mini-tutorial was tested on Mac OS X 10.5.2, and I think it might work on windows and linux too, because it’s more apache stuff.

Did you like this? Share it:

2,100 Comments

So, what do you think ?