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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题