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:

23 Comments

  • Gerson |

    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 |

    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 |

    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 |

    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 |

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

  • Gerson |

    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 |

    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)

  • Ben |

    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 |

    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 |

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

    Thanks, Ben!

  • Mike |

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

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

  • Jonathan |

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

  • Mariusz |

    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 🙂

  • Ellkeycle |

    Cialis (Tadalafil)No Presciption Needed Buy Doxycycline North Ayrshire viagra Cialis Il Costo Hplc Amoxicillin Validation Cialis Preise Ohne Rezept

So, what do you think ?