Fixing CodeIgniter and .htaccess issues on Leopard

by Vicente Russo Neto on May 29, 2008

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
##
# 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

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:

1
#LoadModule userdir_module libexec/apache2/mod_userdir.so

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

1
LoadModule rewrite_module libexec/apache2/mod_rewrite.so

and

1
2
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf

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:

1
NameVirtualHost *:80

Here is the root of your sites

1
2
3
4
<VirtualHost *:80>
    DocumentRoot "/Users/<your-user>/Sites"
    ServerName localhost
</VirtualHost>

And here your Virtual Hosts:

1
2
3
4
5
6
7
8
9
10
<VirtualHost *:80>
    DocumentRoot "/Users/<your-user>/Sites/<testsite1-dir>"
    ServerName testsite1.dev
    <Directory "/Users/<your-user>/Sites/<testsite1-dir>">
        AllowOverride All
        Options +FollowSymLinks
        Order allow,deny
        Allow from All
    </Directory>
</VirtualHost>

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

1
2
AllowOverride All
Options +FollowSymLinks

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:

1
2
3
4
5
6
7
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]

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:

{ 18 comments… read them below or add one }

Nathan Colgate September 25, 2008 at 7:14 pm

Great first post. The bit about putting a directory node in the http-vhosts.conf file put an end to a great deal of frustration.

Diego November 1, 2008 at 5:52 pm

Thank you! works neat :D

Gerson January 29, 2009 at 11:40 pm

I did everything exactly as you said but I got an “Failed to Connect” error: Firefox can’t establish a connection to the server at testsite1.dev. (or localhost or whatever). What can it be?

admin January 30, 2009 at 1:24 am

Gerson, vou responder em portugues já que você é daqui =)

Bom, a princípio eu diria que você não alterou o arquivo hosts… você tem certeza que alterou? Qual versão do Leopard você usa? E do apache?

Gerson January 30, 2009 at 9:09 pm

Escrevi em ingles pra manter a documentação pros gringos… mas va la: Alterei sim… Na verdade o meu estava bem diferente disso, com isso:

ServerAdmin webmaster@dummy-host.example.com
DocumentRoot “/usr/docs/dummy-host.example.com”
ServerName dummy-host.example.com
ServerAlias http://www.dummy-host.example.com
ErrorLog “/private/var/log/apache2/dummy-host.example.com-error_log”
CustomLog “/private/var/log/apache2/dummy-host.example.com-access_log” common

ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot “/usr/docs/dummy-host2.example.com”
ServerName dummy-host2.example.com
ErrorLog “/private/var/log/apache2/dummy-host2.example.com-error_log”
CustomLog “/private/var/log/apache2/dummy-host2.example.com-access_log” common

Comentei tudo e copiei/colei seu codigo trocando so o por gerson q é minha pasta aki (na verdae fiz tudo igualzinho). Mas o resultado foi nao poder mais acessar o site mesmo por localhost (recebo um “Address Not Found” na cara). Tentei diferentes combinações de comentar/descomentar as linhas do mod_userdir.so e do httpd-vhosts.conf mas o resultado é sempre o mesmo: Not Found.

Estou rodando OSX 10.5.6 e o Apache2 built-in do OSX (se nao me engano é o 2.2).

Alguma idéia?

Gerson January 30, 2009 at 9:51 pm

Funcionou! Inexplicavelmente, no localhost ele deu certo agora (pq antes eu ja tinha tentado e nada tambem). Tentei também o testsite1.dev e funciou agora. Não sei explicar o que aconteceu, não mexi em nada. Pelo jeito nunca vou saber o que estava havendo de errado. Anyways, fica valendo o que vc escreveu originalmente. Funciona!

Gerson January 30, 2009 at 10:00 pm

Descobri o que eu fiz. Deixei o mod_userdir.so descomentado. Não sei porque isso ajudou a resolver o problema.

Gerson January 30, 2009 at 10:02 pm

Tentei repetir o mesmissimo procedimento no meu MacBook Air e nao funciona. Cara, nao da pra entender oq diabos acontece… os dois estao iguaizinhos conferido linha por linha…

Gerson January 30, 2009 at 10:07 pm

Burrice: esqueci do .htaccess no MacBook. Funcionou somente com o mod_userdir.so ativo mesmo. Te devo uma (a proposito desculpa a montueira de comentarios :P )

admin February 8, 2009 at 11:53 pm

Maravilha!! :)

Ben July 20, 2009 at 2:15 am

I found a lot of this largely unnecessary:
404 and 500 errors were happening with me as well,using http://localhost/~ website. All I changed was /etc/apache2/users/.conf, by adding FollowSymLinks to Options line, and changing AllowOverride to All. No changes to httpd.conf (I wasn’t comfortable with allowing security changes to the server document root at /). Also, make sure in .htaccess file you set the correct rewriting base: RewriteBase /~/

Gerson September 11, 2009 at 10:23 am

Hi Ben!

What if my /etc/apache2/users/ folder is empty? I just updated to Snow Leopard (OSX 10.6) and I was wandering to care more about security as well. But there’s no .conf file on the users folder and I have no idea what uses to go inside these files…

Could you please explain better your approach?

Derek September 20, 2009 at 10:37 am

Ben’s solution (2 posts up) worked brilliantly. I was getting a “Forbidden” error, even with the correct RewriteBase specified.

Thanks, Ben!

Mike October 3, 2009 at 6:32 pm

Would also like to thank Ben, worked great for me as well.

Thanks to Vicente as well for getting everyone down the path.

Jonathan October 22, 2009 at 3:46 pm

Thank you! I had it working up until the AllowOverride bit. That did the trick!

youtube November 1, 2010 at 1:37 am

Maravilha!! +1 ;) thanks

DouG Molidor December 14, 2010 at 6:35 am

Adding the node as you have worked for me. Thanks!

Mariusz June 15, 2011 at 9:26 am

Hello,
I have a problem with mod_rewrite on my apache server and codeigniter.
Here is my .htaccess:
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]

My httpd-vhosts.conf:

DocumentRoot “D:/xampp/htdocs/ci”
ServerName ci

AllowOverride All
Options +FollowSymLinks
Order allow,deny
Allow from All

config.php:
$config['base_url'] = ‘http://ci/';
$config['index_page'] = ”;

I have controller test.php. When I put http://ci/test, it redirect me to http://ci/test/?/test

If I don’t use a vhost and put localhost/ci/test it works well.

I hope you can help me :)

Leave a Comment