HTTP 404 and 500 problem with CodeIgniter and Apache


So, a certain website that our department supposed to manage was hosed up. Apparently it was a VPS node that noone was aware of, and one that we have lost control of. We managed to pull out the website codes from the original server via cpanel, and score a secondary node, and load apache, php, and mysql. The node uses cpanel, and since I’m not familiar with one, doing these stuff took a little bit longer that I expected. The codes were then transferred to the new nodes.  And as we prepared to nuke the old node from orbit, something annoying happened.

codes

The setup was that, when the index page is loaded up, it will automatically redirect the browser to a pre-selected language, which is indonesian, to the “/id” directory. For example, when you open up http://www.your.site”, the index page will instead load http://www.your.site/id”. The problem is, instead of loading the proper page, apache shows a 404 error, saying it can’t find “/id” on the server.

So it’s either an alias on apache was not properly set, or the website is doing an URL rewrite. Since I can’t log into the original webserver, I can’t see whether it uses Alias or not, or if it did, where “/id” should be pointed to.

Looking deeper into the website source, I realized that it was built using CodeIgniter, a small, lightweight framework for building web application. And it must have mod_rewrite enabled.

First make sure that mod_rewrite is loaded. To do that on an Ubuntu box, do

ikhsan@slaveE:~$ sudo apache2ctl  -M | grep rewrite

On RHEL and its’ derivatives do

[ikhsan@aeontr ~]$ sudo apachectl  -M | grep rewrite

Finally on an cpanel “infected” (sigh) instance, do:

[ikhsan@walt ~]$ sudo /usr/local/apache/bin/apachectl -M | grep rewrite

The output should be similar to this

[ikhsan@walt ~]$ apachectl -M | grep rewrite
 rewrite_module (shared)
Syntax OK

If it’s not loaded up on your box, do it now. Ubuntu user can do

ikhsan@slaveE:~$ sudo a2enmod rewrite

CPanel’s Apache distribution is compiled with mod_rewrite built in and RHEL derivatives distro users can instead open their httpd.conf, and find and uncomment this line

LoadModule rewrite_module modules/mod_rewrite.so

restart apache, and check again.

Now that the mod_rewrite situation has been taken care of, let’s move on to the next step. CodeIgniter’s installation requires us to set a .htaccess file. Navigate to where your CodeIgniter resides, and create a .htaccess file with your preferred text editor. Put these lines inside

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

Save the file. Now heads up to where your httpd.conf or your Ubuntu equivalents, open it up, and navigate to the directory settings. Make sure that on the directory setting where CodeIgniter resides, “AllowOverride” is set to All. For example:

<Directory "/usr/local/apache/htdocs">
 Options All
 AllowOverride All
 Require all granted
</Directory>

Save, and restart apache. URL rewrites on CodeIgniter should now works.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s