当在 url 末尾添加斜杠 (/) 时,codeigniter 加载 css、js、img 文件时出现奇怪的行为

发布于 2024-11-26 00:17:15 字数 1424 浏览 0 评论 0原文

我非常了解 Codeigniter,但在尝试将静态站点转换为 CI 时遇到了这个问题。当使用某些 url 时加载 css 文件时会出现此问题。我试图严格让 php 远离视图(事实上只想有 .html 页面用于视图)。

我还搜索了很多论坛来寻找加载CSS的解决方案。我遇到过一些解决方案,但只有当您在 CI 中创建一个全新的站点时它们才有帮助。我知道

在 href 链接之前使用它可以使其独立于站点,

但不想使用它,因为我想将现有的静态网站转换为 CI。这意味着更改每个文件中的每个 href。 (如果有很多页面,这似乎是一场噩梦)。

我在某些情况下加载 css 和其他资源文件时遇到问题。下面是我迄今为止在该项目中所做的一切的描述。

  1. 在根目录下使用 .htaccess 删除“index.php”文件
  2. 使用 codeigniter 2.0.2 附带的标准文件夹结构
  3. 所有图像和资源(例如 css、img、js ......等)都直接放置在根文件夹中(即应用程序和系统文件夹所在的位置)
  4. 所有资源都可以从视图文件访问,就好像 html 文件与资源一起位于根文件夹中一样(例如 href="css/style.css")

下面是示例控制器的描述

class Blog extends CI_Controller {

public function index(){
    $this->load->view('blog.html');
    }

public function posts(){
    $this->load->view('post.html');
    }
}

现在我面临的问题是当我使用某些 url 作为“index”函数时。 。 css 已正确加载。

但是如果我在那之后添加斜杠或使用任何其他函数。加载 css 时出现问题。

例如。

 /* This url loads css correctly */
http://localhost/ci-test/blog

 /* these urls do not load css */
http://localhost/ci-test/blog/
http://localhost/ci-test/blog/posts

我做错了什么吗?或者我的 .htaccess 文件有问题? (包含在下面)

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

我需要做什么才能以最小的努力将当前网站导入到 CI 中。

im quite aware of Codeigniter but came across this problem while trying to convert a static site into CI. The problem happens while loading css files when certain urls are used. Im trying to strictly keep php away from views (infact want to have only .html pages for views).

I have also searched many forums for a solution for loading css. I have come across some solutions but they help only if you are making a completely new site in CI. I am aware of

using before the href link to make it site independant

but do not want to use this, cause i want to convert an existing static website into CI. which would mean changing each and every href at every file. (seems like a nightmare if there are many pages).

I am having trouble loading the css and other resource files in certain conditions. Below is a description of what all i have done so far in the project.

  1. used .htaccess at root to remove "index.php" file
  2. used standard folder structure that comes with codeigniter 2.0.2
  3. all images and resources (Eg. css, img, js . . . etc) are placed directly in the root folder (i.e. where the application & system folders are located)
  4. all resources are accessed from the view files as if the html files were in the root folder along with the resources (Eg. href="css/style.css")

Below is a sample description of the controller

class Blog extends CI_Controller {

public function index(){
    $this->load->view('blog.html');
    }

public function posts(){
    $this->load->view('post.html');
    }
}

Now the problem i am facing a problem is when i use certain url for the "index" function . . the css gets loaded correctly.

But if i add a slash after that or use any of the other functions. there is a problem loading the css.

Eg.

 /* This url loads css correctly */
http://localhost/ci-test/blog

 /* these urls do not load css */
http://localhost/ci-test/blog/
http://localhost/ci-test/blog/posts

Is there something i am doing wrong? or some problem with my .htaccess file ? (included below)

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

What do i need to do to import the current website into CI with minimal effort.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺渁∝ 2024-12-03 00:17:15

你可能会尝试这样的事情:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA] 

不确定最后一条规则是否有效,因为我不知道 CI 的路由是如何工作的,但它是我习惯使用 Zend 和 Symfony 路由器的。

You might try something like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^(index\.php|images|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA] 

Not sure if that will last rule will work because i dont know how CI's routing works, but its what im used to with Zend and Symfony routers.

薄暮涼年 2024-12-03 00:17:15

从视图文件访问所有资源,就好像 html 文件与资源一起位于根文件夹中一样(例如 href="css/style.css")

好吧,如果浏览器看到的外部 URL 可能会这样,那么这是行不通的有很多层嵌套目录,可以吗?要使相对 URL 在 ci-test/blog 中工作,您必须引用 css/style.css,而要使其在 ci-test/ 中工作blog/posts 它只能是 ../css/style.css

我建议将所有引用替换为根 URL,例如 /ci-test/css/style.css,或者如果使用 中的类似内容,使所有相对 URL 的行为与根 URL 相关。

all resources are accessed from the view files as if the html files were in the root folder along with the resources (Eg. href="css/style.css")

Well, that can't work if the external URL the browser sees may have many levels of nested directories, can it? To make a relative URL work from ci-test/blog you would have to reference css/style.css, whereas to make it work from ci-test/blog/posts it could only be ../css/style.css.

I suggest replacing all the references with rooted URLs like /ci-test/css/style.css, or if that's too much work using a <base href="/ci-test/"> or similar in the <head> to make all relative URLs behave as related to a rooted URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文