使用.htaccess mod_rewrite绕过深层目录结构到CSS资产

时间:2010-11-17 02:12:07

标签: apache .htaccess mod-rewrite

我有这样的设置

  

http://localhost/
  http://localhost/ci_tada/
  http://localhost/ci_tada/application
  http://localhost/ci_tada/application/views
  http://localhost/ci_tada/application/views/css
  http://localhost/ci_tada/application/views/css/master.css

我不想写

  

http://localhost/ci_tada/application/views/css/

每次我想访问一个css文件(同样适用于图像等)

我希望能够使用 http://localhost/ci_tada/css/master.css

并加载正确的文件。

.htaccess文件位于ci_tada文件夹中。

2 个答案:

答案 0 :(得分:0)

看起来你在框架中使用某种路由。您应该检查,因为某些框架为您提供了在应用程序配置中执行此操作的选项。

<IfModule mod_rewrite.c>
RewriteEngine On

# directory base, untogle if you want to
# rewrite only starting from /<directory/path>
# RewriteBase /

RewriteRule ^/css/(.*)$ index.php/ci_tada/application/views/css/$1 [PT,L]
#or RewriteRule ^/(.*)/css(.*)$ index.php/$0/views/css$1 [PT,L]
#or RewriteRule ^/(.*)/css/(.*)$ index.php/$0/views/css/$1 [PT,L]

</IfModule>

Beware that the last two redirect everything that contains /css/ in the path.


edit:1: It is considered best practice in CI (from what I've read), to set a static directory on your root like this:


    /
    .../static
    ......./css
    ......./js
    .../application
    ......./controller
    .../...

<IfModule mod_rewrite.c> RewriteEngine On # directory base, untogle if you want to # rewrite only starting from /<directory/path> # RewriteBase / RewriteRule ^/css/(.*)$ index.php/ci_tada/application/views/css/$1 [PT,L] #or RewriteRule ^/(.*)/css(.*)$ index.php/$0/views/css$1 [PT,L] #or RewriteRule ^/(.*)/css/(.*)$ index.php/$0/views/css/$1 [PT,L] </IfModule> 内嵌您的观点。如果他们可以提供帮助,请参阅这些资源:

答案 1 :(得分:0)

我觉得你在谈论Zend Framework。你的几行是

/

/应用

如何将每个请求重定向到CSS /?那么你的申请将无法运作!

在Zend Framework,application / layouts / scripts / layout.phtml中,你提到了这样的css文件:

<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>

Css文件进入项目文件夹。它在应用程序文件夹之外: \ quickstart2 \公共\ CSS。

希望我不会收到-ve answer。