.Htaccess for Cakephp

.htaccess for cakephp

The answer is that there are 3 different .htaccess files:

/var/www/app/webroot/.htaccess

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

/var/www/app/.htaccess

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

/var/www/.htaccess

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

It's been my fault, everything is listed on the CakePHP site. Thanks to everyone!

CakePhp 2.0 .htaccess

As suspected in my edit, the problem was related to the server running cgi php. I'm not exactly sure what the issue was, but the CakePhp google group provided this solution:

In app/Config/core.php there is a commented out definition for App.baseUrl. Uncomment it and replace with the following

Configure::write('App.baseUrl', '/mysite/');

Above it, there are comments saying to remove the .htaccess files that you should ignore. The site worked fine with the .htaccess files as their standard values, no RewriteBase needed. The one annoyance was that $this->Html->css() and $this->Html->js() functions did not work, but I only call them in templates, so it's not terrible to hard code them.

If anyone has insight into the cause of these cgi woes, please share. It still feels a bit like a kludge if I can't use all of the Html helpers.

.htaccess / mod_rewrite.c : CakePHP application in a subfolder where the root folder hosts a Wordpress installation

The following works for mydomain.com/cakeapp:

(0) .htaccess in ROOT FOLDER

#Action php /cgi-php52/php
#AddHandler php52 .php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^cakeapp/(.*)$ /cakeapp/$1 [L,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

(1) .htaccess in the CAKEAPP-Folder

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

(2) .htaccess in the ROOT/CAKEAPP/APP-Folder

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

(3) .htaccess in the ROOT/CAKEAPP/APP/WEBROOT-Folder

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cakeapp
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

redirect with .htaccess with cakePHP

Try instead of using $1, using the entire request URI:

RewriteRule ^(.*)$ https://%1%{REQUEST_URI} [R=301,L]

.htaccess rules to have wordpress in root and cakephp in subfolder

In the directory where WordPress is installed, i editing the .htaccess file and i have add the following line. (CakePHP subdirectory will be called "cake")

Wordpress .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^cake/(.*)$ /cake/$1 [L,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

/cake .htaccess

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

/app .htaccess

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

/app/webroot .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /cake
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

That’s it. You’re good to go.

Visiting example.com in the browser will open your WordPress website, while example.com/cake/, depending on how you configured it, will open your CakePHP app.

CakePHP 3 htaccess ignore directory

Besides the required / as mentioned by @GregSchmidt, rewrite conditions only apply to the first rule that is following them, so your config basically says:

IF (URI != ^/trackparser/.*$) {
RewriteRule ^(\.well-known/.*)$ $1 [L]
}

RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]

What you want is to apply all three rules if the URI doesn't match ^/trackparser/.*$. To achieve that you can invert the condition and use a skipping rule, which when applied, will skip N number of the subsequent rules:

RewriteRule ^(\.well-known/.*)$ $1 [L]

RewriteCond %{REQUEST_URI} ^/trackparser(/.*|$)
RewriteRule . - [S=2]

RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]

This will skip the 2 CakePHP webroot rules in case the condition is met, so it's basically doing:

RewriteRule ^(\.well-known/.*)$ $1 [L]

IF (URI != ^/trackparser(/.*|$)) {
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
}

Note the removed negation operator, and the added (/.*|$) to make the ending slash optional. The rule for the well-known directory (certificate validation) could theoretically also be skipped, but I'd make it exempt in order to avoid it accidentally not being applied because of later changes.

See also

  • Apache > HTTP Server > Documentation > Version 2.4 > Modules > Apache Module mod_rewrite > RewriteCond Directive
  • Apache > HTTP Server > Documentation > Version 2.4 > Rewrite > RewriteRule Flags > S|skip


Related Topics



Leave a reply



Submit