Asked  8 Months ago    Answers:  5   Viewed   40 times

I'm trying to get a CakePHP application to work. For this, I've set up a brand new Debian installation, updated the configuration and put everything in /var/www, which has the following content:

app
cake
.htaccess
index.php
vendors

The .htaccess file contains the following:

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

If I access my virtualhost (http://myhost/) I see the correct page. But even the JavaScript loaded with src="/js/validate.js" fails (it's located within /var/www/app/webroot/js/validate.js):

[Wed Aug 26 15:45:12 2009] [error] [client 10.7.10.52] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Wed Aug 26 15:45:12 2009] [debug] core.c(3063): [client 10.7.10.52] r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /webroot/js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] core.c(3069): [client 10.7.10.52] redirected from r->uri = /js/prototype.js
[Wed Aug 26 15:45:12 2009] [debug] mod_deflate.c(632): [client 10.7.10.52] Zlib: Compressed 649 to 405 : URL /webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/webroot/js/prototype.js

Hence my question: what is the correct .htaccess required for CakePHP?

Many, many thanks!

 Answers

59

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!

Wednesday, March 31, 2021
 
GGio
answered 8 Months ago
52

Seems like the solution that worked for me was to edit /etc/apache2/users/username.conf and add this at the top:

DocumentRoot "/Users/username/Sites"

And the default cakePHP download now runs ok.

Wednesday, March 31, 2021
 
mario
answered 8 Months ago
49

The best way to achieve this would be with Ajax, or JS/Jquery.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#submit").click(function () {
            $.post($("#addToDB").attr("action"), $("#addToDB").serialize(),
              function () {
                  alert('Add to Database submitted');
              });

            $.post($("#paymentGateway").attr("action"), $("#addToDB").serialize(),
              function () {
                  alert('Payment Gateway submitted');
              });
        });
    });
</script>

The form:

<form id="addToDB" action="addtodatabase.php" method="post">
    <input type="button" id="submit" value="Submit" />
</form>

<!-- Hidden form for payment gateway -->
<form id="paymentGateway" action="paymentgateway.php" method="post">
    <!-- Payment gateway input fields -->
</form>

Clicking the button with id="submit" calls function to post both forms by form id.

Wednesday, March 31, 2021
 
Blacksonic
answered 8 Months ago
69

Can someone chime in and help me out with the "path" this application is taking before running any php files? Is it redirecting everything to cakeappwebroot, then to whatever redirect is setup beyond the .htaccess files from there? (in this case, to cakeuserslogin)

In short, yes. There is only one .htaccess file doing the real work, and that is "C" (which is the file in /app/webroot/.htaccess). This passes any requests that aren't for an existing file or directory to index.php which bootstraps CakePHP and handles the request. Any further "redirection" is handled by CakePHP's routing.

Edit: To address your question about what's redirecting to the login page, chances are you have configured the Auth component and haven't set any "public" pages via $this->Auth->allow().


The other two are there in case you put the CakePHP installation directory or the app directory into a folder that is used by Apache to serve pages, e.g. /var/www/html or similar. You'll note that the structure is:

/cakephp-1.3.x/.htaccess ("A")

/cakephp-1.3.x/app/.htaccess ("B")

/cakephp-1.3.x/app/webroot/.htaccess ("C")

So, any request at any of these levels will end up being handled by the RewriteRule in "C". This is done to protect sensitive data such as your database connection information and ensure the application functions properly (as all requests should go through the CakePHP bootstrapper, unless you've set up custom routing).

Saturday, May 29, 2021
 
eliotlencelot
answered 5 Months ago
31

I guess you have been upgrading to CakePHP 3.2, otherwise you'd have seen in your config/bootstrap.php file that there are separate types for DATE, DATETIME and TIME type columns.

With CakePHP 3.2 the date type doesn't mape to CakeI18nTime anymore, but to CakeI18nDate (or CakeI18nFrozenDate when instructed to use immutable objects), and it needs to be configured separately, which is why changing the datetime type, or the CakeI18nTime class config won't affect your DATE columns.

To configure formatting for the latter, use the CakeI18nDate and/or CakeI18nFrozenDate class and the date type. In your bootstrap, you could do something like

ini_set('intl.default_locale', 'pl_PL');

// ...

CakeI18nDate::setToStringFormat('yyyy-MM-dd');
CakeI18nFrozenDate::setToStringFormat('yyyy-MM-dd');

CakeDatabaseType::build('date')
    ->useImmutable()
    ->useLocaleParser()
    ->setLocaleFormat('yyyy-MM-dd');

That would override the defaults that are being applied when using the pl_PL locale. Note that you should use yyyy instead of YYYY (as the latter defines the week-numbering year), and MM instead of mm (as the latter defines minutes).

See https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax for the format used by the intl formatter that CakePHP uses internally.

Also note that there's also CakeI18nDate::$wordFormat and CakeI18nDate:$niceFormat which you may want to change too.

See also

  • Source > cakephp/app > config/bootstrap.php
  • API > CakeI18nDateFormatTrait::i18nFormat()
  • API > CakeI18nDate::$wordFormat
  • API > CakeI18nDate::$niceFormat
  • Cookbook > Date & Time > Immutable Dates and Times
  • Cookbook > Date & Time > Setting the Default Locale and Format String
  • Cookbook > Internationalization & Localization > Parsing Localized Datetime Data
Friday, July 30, 2021
 
David
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share