user117643
user117643

Reputation: 111

fixing cakephp .htaccess/mod_rewrite for shared hosting setups

I'm trying to install cakePHP on a shared hosting setup. After extracting the files to a location like ~/public_html/dev/cake and visiting the appropriate URL (in this case http://hostname/~username/dev/cake/), I receive a 404 error:


Not Found

The requested URL /usr/home/username/public_html/dev/cake/app/webroot/ was not found on this server.


I suspect that the reason for this is that upon closer inspection, the absolute path to ~/public_html is not in fact /usr/home/username/public_html, but rather /usr/www/users/username/.

Here's what I've been trying (but obviously it's not working): (~/public_html/dev/cake/app/webroot/.htaccess)

<IfModule mod_rewrite.c>  
    RewriteEngine On  
    RewriteBase /usr/www/users/username/dev/cake/app/webroot/ 
    RewriteCond %{REQUEST_FILENAME} !-d  
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]  
</IfModule>  

Unfortunately, this doesn't seem to change anything (the 404 message remains that same). Thoughts?

Upvotes: 11

Views: 23495

Answers (8)

Joe
Joe

Reputation: 21

I just want to point out that I THOUGHT the module was loaded because I saw the code but apparently it was not enabled!

To enable it in SSH (terminal) type the following command

sudo a2enmod rewrite

The command below shows all modules that are active, if it appears in the list then you have it installed.

apache2ctl -M

Make sure to restart the server with...

service apache2 restart

PhpCake should start working now.

Upvotes: 2

Vijay Kumbhar
Vijay Kumbhar

Reputation:

Yes, you have to give the DOCUMENT_ROOT path in the following files. Please refer to phpinfo() of your server to get correct DOCUMENT_ROOT path. It should be something like var/www/html/.

  1. .htaccess in the root / directory (Outside the app,cake,vendors directories)

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$ ) DOCUMENT_ROOT/app/webroot/    [L]
        RewriteRule    (.*) DOCUMENT_ROOT/app/webroot/$1 [L]
    </IfModule>
    
  2. .htaccess inside the /app directory

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule    ^$    DOCUMENT_ROOT/app/webroot/    [L]
        RewriteRule    (.*) DOCUMENT_ROOT/app/webroot/$1    [L]
    </IfModule>
    
  3. .htaccess inside the /app/webroot directory

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ DOCUMENT_ROOT/app/webroot/index.php?url=$1 [QSA,L]
    </IfModule>
    

It worked for me.

Upvotes: 1

Copter
Copter

Reputation: 21

I had similar problem with 'advanced installation' from the official CakePHP Cockbook. After couple of hours I managed to get it working.

OS: Mac OS X

Cake core directory: /usr/lib/cake
My application app directory: /Users/myuser/dev/myapp
My application webroot directory: /Users/myuser/Sites/myapp

Modification of /Users/myuser/Sites/myapp/index.php

if (!defined('ROOT')) {
    define('ROOT', DS.'Users'.DS.'myuser'.DS.'dev');
}
if (!defined('APP_DIR')) {
    define('APP_DIR', 'myapp');
}
if (!defined('CAKE_CORE_INCLUDE_PATH')) {
    define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib');
}

Modification of /Users/myuser/Sites/myapp/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /~myuser/myapp/
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

As for /Users/myuser/dev/myapp/.htaccess - this file is not needed in this configuration so I deleted it. The most tricky part with this configuration is enabling mod_rewrite in Apache. On Mac editing /private/etc/apache2/httpd.conf seems to be pointless (for this task). Additional Apache configuration file is created for each user.

Modification of /private/etc/apache2/users/myuser.conf

<Directory "/Users/myuser/Sites/">
    Options FollowSymLinks
    AllowOverride All
</Directory>

By default AllowOverride is set to None, so mod_rewrite will not work in this configuration.

After this changes http://localhost/~myuser/myapp works properly.

PS. Quick permission fix for tmp directory.

sudo chgrp -R www /Users/myuser/dev/myapp/tmp
sudo chmod -R g+w /Users/myuser/dev/myapp/tmp

Upvotes: 1

Paul
Paul

Reputation:

I am hosting on pair and I was able to get the rewrite rules to work but, I am having problems with the HTML->link getting the sort parameters for all links in the pages. Same code runs fine on my local machine running window but on pair sorting or too many query parameters (Edit or Update pages) causes problems.

Upvotes: 1

artlung
artlung

Reputation: 33833

Shouldn't

RewriteBase /usr/www/users/username/dev/cake/app/webroot/ 

be

RewriteBase /~username/dev/cake/

RewriteBase cares about mapping real urls to virtual ones, and I think that's what you want.

Upvotes: 13

Simon
Simon

Reputation: 950

You're modifying the wrong file. The .htaccess file you need to modify is found in /dev, you shouldn't be modifying the one in /dev/cake/app/webroot/.htaccess

Upvotes: 2

zam3858
zam3858

Reputation:

i think the .htaccess is reading that '~' and thinking you are refering to your home folder (which is something of a shortcut in linux).

fastest way to get your app running is to go to cake's config/core.php and disable use of rewrite (it tells u to uncomment some stuff and delete some .htaccess file). the other way is to edit your .htaccess and somehow tell it '~' is not refering to home directory. maybe a simple \ will work.

Upvotes: 1

David Z
David Z

Reputation: 131800

Why not just extract the files to /usr/www/users/username/?

Upvotes: 1

Related Questions