不包括 CodeIgniter CSS 文件

发布于 2024-10-09 11:20:52 字数 1090 浏览 0 评论 0原文

我的 CodeIgniter 安装有一些奇怪的问题,我使用 modrewrite 来缩短我的 URL,例如我有一个 api 页面:

http://www.mydomain.com/api

它工作得很好,我的 .htaccess 文件看起来像这个:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

但真正奇怪的是,如果我输入一个尾部斜杠:

http://www.mydomain.com/api/

API 页面加载正常,但无法加载包含所有页面中包含的基本 CSS 文件,如果它根本不加载任何内容就很好,但我需要阻止它加载实际的 api 页面。任何建议都会有帮助,

谢谢!

更新:我发现当我查看源代码时,当 CSS 正确加载时,我的 CSS 文件来自以下内容:

http://www.mydomain.com/css/all.css< /p>

但是当它失败时加载它来自

http://www.mydomain.com/api/css/all.css< /a>

I have a bit of an odd problem with my CodeIgniter installation, I am using modrewrite in order to shorten my URLs, for example I have an api page:

http://www.mydomain.com/api

And it works really well, my .htaccess file looks like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

But whats really strange is if I type a trailing slash:

http://www.mydomain.com/api/

The API page loads ok but it doesn't include the Base CSS file which is included on all the pages, it would be fine if it didn't load anything at all, but I need to stop it from loading the actual api page. Any advice would be helpful,

Thanks!

UPDATE: I have found that when I view source my CSS file comes from the following when the CSS loads correctly:

http://www.mydomain.com/css/all.css

But when it fails to load it comes from

http://www.mydomain.com/api/css/all.css

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

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

发布评论

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

评论(2

梦断已成空 2024-10-16 11:20:52

确保 config.php 文件中的 base_url 中有一个尾部斜杠:

$config['base_url'] = "http://www.mydomain.com/";

并且在处理链接时始终调用 base_url() ...等

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

编辑:
请注意,我正在使用此 wiki 页面 中的 mod_rewrite 但自从我的 CI 安装不在根目录(在 /ci173/ 内),RewriteBase 不是 / 而是 /ci173/ >。

Make sure that you have a trailing slash in your base_url in the config.php file:

$config['base_url'] = "http://www.mydomain.com/";

And always call the base_url() when dealing with links...etc

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

EDIT:
Please note that I'm using the mod_rewrite from this wiki page BUT since my CI installation in not on the root (inside /ci173/) the RewriteBase is not / but /ci173/.

故事与诗 2024-10-16 11:20:52

当使用像 css/all.css./css/all.css 这样的相对 URL 路径(两者是等效的)时,您需要注意 相对 URL 路径(与任何相对 URL 一样)使用基本 URL 进行解析,即 URL如果没有另外指定,则为当前文档。

因此,在您的情况下 /api/api/ 是基本 URL 路径,相对 URL 路径 css/all.css 的解析方式不同,具体取决于在基本 URL 路径上:

  • /api + css/all.css/css/all.css
  • /api/ + css/all.css/api/css/all.css

要解决这个问题,您需要

  • 使用绝对 URL 路径,例如 /css/all .css 独立于基本 URL 路径
  • 在 HTML 文档中指定不同的基本 URL(例如 /);但请注意,这将影响所有相对 URL,而不仅仅是相对 URL 路径,
  • 调整您的相对 URL 路径以适应您的基本 URL 路径:
    • 对于/api使用css/all.css
    • 对于/api/使用../css/all.css
    • 对于 /api/foo/bar/ 使用 ../../../css/all.css

我想第一个解决方案是最简单的。

When using relative URL path like css/all.css or ./css/all.css (both are equivalent), you need to be aware that relative URL paths (like any relative URLs) are resolved using a base URL that is the URL of the current document if not specified otherwise.

So in your case /api or /api/ is the base URL path and the relative URL path css/all.css is resolved differently depending on the base URL path:

  • /api + css/all.css/css/all.css
  • /api/ + css/all.css/api/css/all.css

To conquer this you either need to

  • use absolute URL paths like /css/all.css that are independent from the base URL path
  • specify a different base URL in your HTML document (e.g. /); but note that this will affect all relative URLs and not just relative URL paths
  • adjust your relative URL paths to suite your base URL paths:
    • for /api use css/all.css
    • for /api/ use ../css/all.css
    • for /api/foo/bar/ use ../../../css/all.css, etc.

I guess the first solution is the easiest.

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