Hosting CakePHP 2.x or newer Application to a Free web-hosting site (ex. 000webhost.com)


I got a lot of trouble web hosting my CakePHP applications to a free hosting site. After an hour of searching and tweaking some files, most of it are those .htaccess that you can find in the main folders, I definitely got it to work.

The first step is to copy your project folder to your desktop (or anywhere you like). We will do this to avoid ruining your original files in case something unexpected happens.

The second step is to copy your  app/webroot to the root folder. Rename "webroot" folder to "public_html".

Although this is not necessary, I think this will make it simplier and understandable specially when modifying those .htaccess files.

The third step will be modifying those .htaccess files that can be found in the root, public_html and app directories.


 The first .htaccess you need to modify is /.htaccess and use the code below

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ /public_html/ [L]
RewriteRule (.*) /public_html/$1 [L]
</IfModule>


 The second .htaccess you need to modify is app/.htaccess and use the code below

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule    ^$    /public_html/    [L]
    RewriteRule    (.*) /punlic_html/$1    [L]
</IfModule>


 The third and last .htaccess you need to modify is public_html/.htaccess and use the code below
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/(app/webroot/)?(img|css|js)/(.*)$
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>


What happens here is that you need to add RewriteBase / in order to set the current directory to the root folder and assign those resources to public_html that is the previous webroot.

The fourth step is to modify public_html/index.php. Change the line to something like below.

/**
 * The full path to the directory which holds "app", WITHOUT a trailing DS.
 *
 */
if (!defined('ROOT')) {
define('ROOT', DS.'home'.DS.'a5285082');
}

/**
 * The actual directory name for the "app".
 *
 */
if (!defined('APP_DIR')) {
define('APP_DIR', 'app');

The last step is to upload your application using FTP tools, I use FileZilla as my FTP tool. 

Now you are good to go!

Tak Note: You only have to upload the files and folder inside your main project folder.

Labels: ,