CodeIgniter HTAccess不加载CSS文件

时间:2011-07-20 07:32:05

标签: css .htaccess codeigniter

我刚刚在我的codeigniter应用程序中添加了一个htaccess文件,该文件从url中删除了“index.php”。 URL工作正常,但现在CSS链接不起作用,即使引用是绝对的(即“http:// ...”)图像以及所有其他链接工作得很好,但CSS文件赢了加载。这是HTAccess文件。

RewriteEngine on RewriteCond $1 !^(main.php|images|robots.txt) RewriteRule ^(.*)$ main.php/$1 [L]

是的,我现在的索引文件是main.php,因为我将在同一CI安装上使用两个不同的应用程序。谢谢你的帮助。

5 个答案:

答案 0 :(得分:11)

您可能还希望尝试以下操作:

# 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 ^(.*)$ main.php/$1 [L]

这将允许您在Web根目录中创建所需的任何文件夹(样式,图像,脚本),而无需在htaccess文件中声明它们。

答案 1 :(得分:2)

您的htaccess文件缺少一行:)

RewriteEngine on
RewriteCond $1 !^(main\.php|images|robots\.txt|styles)
RewriteRule ^(.*)$ /main.php/$1 [L]

您可以将样式目录放在RewriteCond行中。我添加了样式。例如。

更多信息here

答案 2 :(得分:2)

我用这个。评论很好,显然不是我写的。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #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]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    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]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule> 

答案 3 :(得分:1)

您可能需要查看我现在正在使用的 .htaccess 文件。

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css|design|img|js)
RewriteRule ^(.*)$ /a-cat/index.php/$1 [L]
</IfModule>

目录结构如下:

enter image description here

CSS文件可以这种方式使用:

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

答案 4 :(得分:0)

这个问题的简单答案是:使用codeigniter中的.htaccess代码,如下所示,在css,js和images的路径上使用base_url函数,如图所示

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase # The path to your project/official/hrm/

    #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]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    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]


</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

<link rel="stylesheet" href="<?php echo base_url(); ?>the rest of path to yourfile">