Codeigniter + CSS效果不佳,可能是baseurl

时间:2012-12-09 01:36:37

标签: php css .htaccess codeigniter

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

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

css文件位于:

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

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

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

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

那么问题是什么?有人可以帮忙吗?

4 个答案:

答案 0 :(得分:6)

您无法将CSS文件或文件提供给应用程序文件夹中的浏览器,因为出于安全考虑,将.htaccess文件设置为“拒绝所有”您的CSS,JS,图像文件等,需要在应用程序文件夹之外。

将您的CSS文件放在'www'文件夹中名为'css'的文件夹中,这样它就不在'application'或'system'中。然后确保使用以下内容(注意前导斜杠,表示绝对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]

答案 1 :(得分:1)

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

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

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

并使用以下文件结构:

  • WWW

    • 应用

      • 视图
      • (...)
    • 系统

    • 资产
      • CSS
        • screen.css

答案 2 :(得分: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直接访问该文件夹(以及里面的任何文件夹)。

答案 3 :(得分:1)

我在Windows中也面临同样的问题。在Mac中,这很容易。 在尝试从网址中排除index.php时,我遇到了400 Bad Request Error。 后来通过在斜杠RewriteRule index.php前加上来修复此问题,如下所示

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

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

前进,面对另一个问题: 无法加载资源/静态文件。另外,我在根目录级别有我的资源文件夹。 我看到了一个解决方案,比如在RewriteCond中添加'assets'以使其被排除在应用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]

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