4

这个问题已经被问过好几次了,但我研究了仍然无法解决。在我的一个视图文件中,我有(参考我的 css):

link rel="stylesheet" type="text/css"  href="/application/views/screen.css"

css 文件位于:

- www
  - application
    - view
      - css
        - screen.css
  - system

我还尝试在同一文件夹下设置css-www并直接使用它

link rel="stylesheet" type="text/css"  href="css/screen.css"

我的基础url设置为""因为我在本地开发。这是问题吗?我正在使用 wamp 服务器。

那么问题是什么?任何人都可以帮忙吗?

4

4 回答 4

6

您不能将您的 CSS 文件或要提供给浏览器的文件放在应用程序文件夹中,因为出于安全原因,它受到保护,将 .htaccess 文件设置为“全部拒绝”您的 CSS、JS、图像文件等需要在应用程序文件夹之外。

将您的 CSS 文件放在“www”文件夹中名为“css”的文件夹中,使其不在“应用程序”或“系统”内。然后确保使用以下内容(注意前导斜杠,表示绝对 URL):

href="/css/screen.css"

改用这个:

# If your default controller is something other than
# "welcome" you should probably change this
RewriteCond $1 !^(index\.php|css)
RewriteRule ^(.*)$ /index.php/$1 [L]
于 2012-12-09T01:40:07.167 回答
1

请记住,有几种方法可以给猫剥皮。

快速修复而不会弄乱 htaccess。使用 base_url() 或 site_url():

<link rel="stylesheet" type="text/css"  href="<?php echo base_url();?>assets/css/screen.css">

并使用以下文件结构:

  • 万维网

    • 应用

      • 意见
      • (...)
    • 系统

    • 资产
      • css
        • 屏幕.css
于 2012-12-09T01:48:04.650 回答
1

您应该将所有资产文件(图像、js、css 等)放入站点根目录(应用程序文件夹之外)的单独文件夹中。创建一个名为“files”的文件夹,并.htaccess在站点根目录的文件中添加以下行:

# Allow these directories and files to be displayed directly:
# - index.php (DO NOT FORGET THIS!)
# - robots.txt
# - favicon.ico
# - Any file inside of the files/ - js/, or css/ directories
RewriteCond $1 ^(index\.php|files)

这样,您将可以通过 url 直接访问该文件夹(以及其中的任何文件夹)。

于 2012-12-09T14:31:39.567 回答
1

我在 Windows 中也遇到了同样的问题。在 Mac 中,这很容易。当试图index.php从 URL 中排除时,我遇到了400 Bad Request Error. 后来解决了这个问题,通过在 RewriteRule index.php 前面加上斜线前缀,如下所示

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

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

展望未来,面临另一个问题: 无法加载资产/静态文件。另外,我的资产文件夹位于根目录级别。我看到了一个解决方案,例如在 RewriteCond 中添加“资产”以使其被排除在应用 rewriteRule 之外。喜欢,

RewriteCond $1 !^(index\.php|assets)       #It does not work for me

但是,它不起作用。我收到 404 未找到错误。后来,我找到了解决方案,如

RewriteCond $1 !^(index\.php|\/assets)     #Working!

我的重写脚本在这里:

  RewriteEngine on
  RewriteCond $1 !^(index\.php|\/assets)
  RewriteRule ^(.*)$ /index.php/$1 [L]

希望,这将有助于并节省大量时间!:)

于 2017-10-01T06:20:38.267 回答