.htaccess: how do multiple .htaccess files work?

11,911

Solution 1

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 cake\app\webroot, then to whatever redirect is setup beyond the .htaccess files from there? (in this case, to cake\users\login)

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).

Solution 2

The following line redirects everything to your index page, which is a Cake construct.

RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

In your case, I don't think that your redirections are directly caused by your .htaccess files. Your webroot htaccess simply pipes everything to your index page.

You should consider checking your index page, however. The controller for that page probably has a default clause for incoming arguments. Thus, if you enter a url with an unknown parameter, your index page will simply show a login screen.

That's my two cents. However, I've only worked with CakePHP as an experiment, so someone with more experience should feel free to correct me if I'm wrong.

Solution 3

I think it's occurring due to use of Auth component in your application.You wrote that you were trying on Acl component part of cakephp, which is where Auth component is used.In case of inclusion of Auth component in requested controller, cakephp redirects to /users/login by default if no other loin method is specified. So i guess htaccess is not causing problem here.

Share:
11,911
laketuna
Author by

laketuna

Updated on June 04, 2022

Comments

  • laketuna
    laketuna almost 2 years

    This is in context of CakePHP, but I'm sure it is common in other applications. I implemented the instructions on this page:

    http://book.cakephp.org/view/917/Apache-and-mod_rewrite-and-htaccess

    A:

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

    B:

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

    C:

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

    In section 3, there are 3 .htaccess files. Let's just call them A, B, and C in order for simplicity. Although it seems to be working on my server (localhost XAMPP), I'm not sure if I understand the path of the redirects. Whatever address I type in, it always go to localhost\cake\users\login as it's supposed to.

    Root directory of my application in terms of where it resides is \cake. I have \cake, \cake\app, and cake\app\webroot with A, B, and C, respectively, in them. After following the above instruction, the codes in cake\app run, which is what I want. Prior to setting up the .htaccess files correctly like above, codes in \cake were being run; this was incorrect as it was telling me my database wasn't setup, you don't have this file, etc.

    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 cake\app\webroot, then to whatever redirect is setup beyond the .htaccess files from there? (in this case, to cake\users\login)

    Thank you for your help!

    PS: I'm ashamed to say I still haven't figured out which part of which file is redirecting to all of the HTTP request to the user login page. I set this up a while ago, and I just came back to it. I think I was following the second tutorial on the CakePHP Cookbook page where you add an ACL feature..

  • laketuna
    laketuna over 12 years
    Thanks for the input lunchmeat317. When I didn't have the htaccess setup like above and I went to localhost/cake, it was reading files off of the \cake directory where there was no configuration files setup. e.g. it looked foe cake\database.php when the correct one is in cake\app\database.php. I see that cake\app\webroot\index.php is the file every request is redirected to with current A, B, and C setup.
  • laketuna
    laketuna over 12 years
    Hm, I will look into this index.php file more. I don't think I had modified it (using it as-is from the download).
  • laketuna
    laketuna over 12 years
    Thank you so much Justin! You are right. I find the Auth component in cake\app\app_controller.php and the BeforeFilter is at work with the login page. I appreciate the clear explanation. It makes sound sense.