【问题标题】:Where to put resources (css/images) in CodeIgniter? [duplicate]在 CodeIgniter 中将资源(css/图像)放在哪里? [复制]
【发布时间】:2011-11-28 18:28:21
【问题描述】:

我正在使用 CI 进行一些测试,并让它在一个文件夹中运行。以http://www.example.com/sub-folder/ 为例

我的目录结构是这样的(其中/是子文件夹/):
/
|-/系统
|-/应用程序

我有 Eclipse 作为我的 IDE,有 2 个项目,1 个用于系统,1 个用于应用程序,其中应用程序是我的项目,其中包含对系统的引用。

我的 .htaccess 文件阻止访问系统目录。我想将我的图像和 CSS 分别放入:application/resources/images 和 application/resources/css。我希望能够使用<link rel="stylesheet" href="/sub-folder/css/style.css" /> 但是,经过一段时间的测试和搜索,我无法从应用程序目录中加载 CSS 文件。

我需要在 .htaccess 中更改或添加什么才能执行此操作?

我的 .htaccess 的内容如下:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

### Canonicalize codeigniter URLs

# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

【问题讨论】:

    标签: codeigniter mod-rewrite


    【解决方案1】:

    代替

    <link rel="stylesheet" href="/sub-folder/css/style.css" />
    

    你可以的

    <link rel="stylesheet" href="<?php echo base_url('application/sub-folder/css/style.css'); ?>" />
    

    我通常将资产保存在根文件夹(主 index.php 文件所在的位置)并使用 this asset library 加载所有内容。

    如果您想将资产保存在 application/sub-folder 中,您可以这样做:

    【讨论】:

    • 感谢您的回答,但是如果我将资产放入根文件夹,那么它们将位于我的 Eclipse 项目之外,这是我想要避免的。我正在使用base_url('sub-folder/css/style.css'),但为了简单起见,我想我会显示/sub-folder/css/style.css
    • base_url('sub-folder/css/style.css') 将在您的应用程序的根目录中查找 子文件夹。您需要使用base_url('application/sub-folder/css/style.css')。我相应地更新了我的答案。
    • 我试过了,HTML 输出是:&lt;link rel="stylesheet" href="http://www.example.com/sub-folder/application/css/style.css" /&gt; 我的配置有问题吗?
    • 它会翻转应用程序子文件夹吗?这很奇怪。您将 base_url 设置为什么?你试过用site_url() 代替base_url() 吗?
    • 哦,我敢打赌这是您设置的 RewriteBase 的问题。将其设置为“/”将查看服务器的根目录。您可能需要将其设置为“/子文件夹”。
    【解决方案2】:

    我这样设置我的 CI 项目:

    • 应用程序(我的应用程序文件)
    • 资产(可公开访问的 css、js 和图像等)
    • 系统(codeigniter 核心)

    通常,如果我在 CMS 中构建,我会将 CMS 资产放在应用程序文件夹中,因为它们基本上是私有的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2013-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多