Take over the “control” of some urls from CakePHP

Published on and tagged with cakephp  tip

By default, CakePHP takes full “control” of all URLs at the point where it is installed. Sometimes that behaviour is not desired. For example, my hoster defines an url like mydomain.com/stats where the statistics are available. If I install CakePHP in the root, this url no longer works resp. causes an error in CakePHP. The solution is to modify the .htaccess file in app/webroot. The original .htaccess file:

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

And after the modification:

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

The added rule is rather simple: if the url starts with /stats stop the interpretation of the .htaccess file. That’s it.

16 comments baked

  • fraisouille

    Rahhhh I tried so much to make this work !! thanks !!
    I had that problem with my phpmyadmin directory …

  • Daniel Hofstetter (cakebaker): Take over the “control” of some urls from CakePHP

    […] Daniel Hofstetter (cakebaker): Take over the “control” of some urls from CakePHP: ”By default, CakePHP takes full ‘control’ of all URLs at the point where it is installed. Sometimes that behaviour is not desired. For example, my hoster defines an url like mydomain.com/stats where the statistics are available. If I install CakePHP in the root, this url no longer works resp. causes an error in CakePHP. The solution is to modify the .htaccess file in app/webroot. The original .htaccess file: […]

  • oth

    Hmm..Didn’t need that in DH, stats/ and dh_phpmyadmin/

  • cakebaker

    @oth: Hm, I’m also on DH, but for some reason I have to modify the htaccess file in a way similar to the one shown above to access the statistics.

  • Storage Room » Take over the “control” of some urls from CakePHP

    […] Take over the “control” of some urls from CakePHP August 29th 2006 Posted to Internet Clipping, CakePHP http://cakebaker.wordpress.com/2006/08/17/take-over-the-control-of-some-urls-from-cakephp/ By default, CakePHP takes full “control” of all URLs at the point where it is installed. Sometimes that behaviour is not desired. For example, my hoster defines an url like mydomain.com/stats where the statistics are available. If I install CakePHP in the root, this url no longer works resp. causes an error in CakePHP. The solution is to modify the .htaccess file in app/webroot. The original .htaccess file: […]

  • kiri-

    I had the same problem like oth’s and I solved it by modifying not /app/webroot/.htaccess but rather /.htaccess (the htaccess file in the project root).

  • Walid A

    THANKSS SOOO MUCH for this post, Ive been going crazy with this for the last few days.

  • cakebaker

    @Walid: I am glad it was useful for you :)

  • Paul Gardner

    As with kiri- I had to amend the /.htaccess at the root of my cake app to get /awstats to work not /app/webroot/.htaccess

    Thanks for the information though as all my cakephp apps have been without awstats for months :o)

  • cakebaker

    @Paul: I think it depends on the type of installation you have: if you have a production setup, i.e. the document root of your virtualhost points to app/webroot, then only the .htaccess file from app/webroot is used.

    Anyway, I’m glad this article was helpful for you.

  • Paul Gardner

    Thx for the reply, I am not in production mode hence my issue.

    Your post was v useful, although I have a slight problem I wonder if you could cast your eye over.

    I’m only able to login to AwStats using the above method if I have config.debug set to 1 or higher. Once I have authenticated I can set dbug back to 0 and click through as many stats pages as I like so I guess it is something to do with AwStats authentication process.

    Could you give me some pointers as to what I should be checking?

    Thanks, Paul

  • cakebaker

    @Paul: I’m sorry, but right now I don’t have an idea why the debug setting of your Cake app influences the behavior of AwStats…

  • Paul Gardner

    No problem, was worth asking :o)

  • Paul Gardner

    Anyone else having problems where Cake throws 401 errors despite the above rewrite rules if debug is set to 0?

    I now have a few sites live and this is becoming a major issue now that I can’t resolve :(

  • cakebaker

    @Paul: Maybe you will have more luck in the CakePHP irc channel resp. in the Google group…

  • Absolute cakephp URL | Alex Barton

    […] to CakeBaker, you should place the following code in the app/webroot dir .htaccess but this didn’t work […]

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License