【问题标题】:Code Igniter SubDirectory QuestionCodeigniter 子目录问题
【发布时间】:2009-12-19 23:19:35
【问题描述】:

我试图让“trac”目录及其所有子目录都可以通过 url http://www.domain.com/trac/ 访问

我正在使用 codeginiter 框架,我的目录结构看起来像

.htaccess
index.php
system
trac

我可以正常访问 abov url,但问题是 trac 子目录中包含的脚本和其他文件,即:trac/chrome/common/css/trac.css 不可访问和 404。这是我的 .htaccess 代码。请帮忙。

RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^trac/
RewriteRule ^trac/(.*) /trac/$1

【问题讨论】:

    标签: .htaccess mod-rewrite codeigniter


    【解决方案1】:

    您甚至不需要在 .htaccess 中提及 /trac/。正是这一点

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    

    您正在设置两个“重写条件”。第一个说,“只要请求不是文件。”第二个说“或只要请求不是目录”。

    然后

    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    将所有其他内容发送到 index.php,CI 接管。仅作记录,我在每个项目中使用的完整 .htaccess:

    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    

    如果您对 RewriteCond %{REQUEST_URL} ^system.* 行感到困惑,它的存在只是为了让任何浏览器对 /system/ 文件夹的请求始终路由到 index.php,因此被忽略。

    【讨论】:

      【解决方案2】:

      重写规则是按顺序执行的……所以试试这个

      RewriteCond %{REQUEST_URI} !^/trac/
      RewriteCond $1 !(index\.php/)
      RewriteRule ^(.*)$ index.php?/$1 [L]
      

      解释:

      if the uri DOES NOT start with trac
      if the uri IS NOT index.php
      rewrite url as index.php?/{rest of the url}
      

      【讨论】:

        【解决方案3】:

        如果你删除最后两行,它应该可以工作。

        RewriteCond %{REQUEST_FILENAME} !-f 检查以确保您没有请求文件。如果是,则此条件失败,并且永远不会执行规则 RewriteRule ^(.*)$ index.php?/$1 [L]

        【讨论】:

          猜你喜欢
          • 2012-12-20
          • 2013-05-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-19
          相关资源
          最近更新 更多