duanbinren8906 2011-10-25 16:57
浏览 117
已采纳

.htaccess:多个.htaccess文件如何工作?

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

  • 写回答

3条回答 默认 最新

  • duanqianmou4661 2011-10-25 17:41
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗