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

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)