【问题标题】:CodeIgniter HTAccess doesn't load CSS filesCodeIgniter HTAccess 不加载 CSS 文件
【发布时间】:2011-10-09 03:56:50
【问题描述】:

我刚刚向我的 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 安装上使用两个不同的应用程序。感谢您的帮助。

【问题讨论】:

    标签: css .htaccess codeigniter


    【解决方案1】:

    您可能还想尝试以下方法:

    # 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 文件中声明它们。

    【讨论】:

    • 不要忘记 robots.txt 和站点地图。
    • 这与此代码无关,它将检查文件在文件系统中是否有效,如果这些文件不存在,则重定向到应用程序 main.php 文件。这适用于 robots.txt、站点地图——几乎所有内容。
    • 抱歉没注意。炉排解决方案。
    • 对 www.example.com 的调用将获取 index.htm 而不是 index.php
    【解决方案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】:

      您的 htaccess 文件缺少一行 :)

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

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

      更多信息here

      【讨论】:

      • 此外,您必须在此处添加您希望访问的任何 url。否则它们将被重定向到 main.php/$url 页面(当然,它不存在)。
      【解决方案4】:

      您可能想看看我现在正在使用的 .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>
      

      目录结构是这样的:

      CSS文件可以这样使用:

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

      【讨论】:

        【解决方案5】:

        这个问题的简单答案是:使用下面给出的 codeigniter 中的 .htaccess 代码,并在你的 css、js 和图像的路径上使用 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">
        

        【讨论】:

          猜你喜欢
          • 2017-12-22
          • 2015-11-13
          • 1970-01-01
          • 1970-01-01
          • 2012-11-19
          • 1970-01-01
          • 2011-12-14
          • 2014-12-26
          • 2014-01-08
          相关资源
          最近更新 更多