如果我在 index.php 之后输入参数,CSS 样式就会消失

发布于 2025-01-05 10:31:16 字数 1627 浏览 2 评论 0原文

如果我在登陆页面index.php后输入任何参数,我的CSS样式将停止工作

我正在运行xampp的开发本地主机服务器上添加/测试新代码,在php / javascript / html / css中编码。 编辑:我的开发服务器是 Windows Vista,我现在使用 Firefox 版本 10.0.1。

今天我添加了 mod_rewrite 支持,以允许通过我的站点的 URL 传递参数 并偶然发现了一个我以前没有想过要测试的问题。

我正在开发的 mod_rewrite 允许用户在站点名称后指定一个文本字符串,例如:

     http://www.mysite.com/Stonehenge-artifact1

我的 mod_rewrite 处理该参数并执行一些有意义的操作 - 具体来说,它从我的登陆页面 index.php 重定向到一个完全不同的页面显示 Stonehenge-artifact1 的照片。

问题 - 如果用户输入 www.mysite.com/index.php/ialkselasdfa 我所有的 CSS 样式都消失了——CSS 样式表中的 index.php 登陆页面特殊字体和颜色消失了。

我取消了所有 mod_rewrite 更改,包括完全删除 mysite.com 文件夹空间中的自定义 .htaccess 文件(使用 .htaccess 的本地自定义副本是 mod_rewrite 的步骤之一)。

我想看看 CSS 样式是否会消失,因为我回到了昨天的(mod_rewrite 之前的更改)代码。果然,如果我在 index.php 之后输入参数(任何参数),我的 CSS 样式表就会擅离职守。

因此,以下 URL(例如)以某种方式禁用了我的 index.php 的 CSS 样式表

www.mysite/index.php/foo
www.mysite/index.php/css-whereAreYou

(注意:我提出 mod_rewrite 的原因是为了解释为什么我首先在​​网站 URL 的末尾输入一些内容。是吗?不适合我测试新的 mod_rewrite 代码,我不会发现这个问题。)

这是我在我的 CSS 样式表的 index.php 中包含的内容:

<html>
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <link rel="stylesheet" type="text/css" href="globalStyles.css" />
 </head>

我的 globalStyles.css 是:

root { 
  display: block;
}

body
{
   color: black;
   font-family: Gill, Helvetica, sans-serif;
   font-size: 9pt;
   background-color: RGB(243,239,240); /* light blue */
} 

p
{
    color: black;    
}

为什么我的 index.php 登陆页面没有获取 CSS样式时我在index.php后添加字符?

My CSS styles stop working if I type any argument following my landing page index.php

I'm adding/testing new code on my development localhost server running xampp, coding in php/javascript/html/css.
EDIT: my development server is Windows Vista, and I use Firefox version 10.0.1 right now.

Today I was adding mod_rewrite support to allow passing an argument with the URL for my site
and stumbled on a problem I'd not thought to test before.

The mod_rewrite I was developing allows the user to specify a text string after the site name, such as:

     http://www.mysite.com/Stonehenge-artifact1

and my mod_rewrite processes that argument and does something meaningful -- specifically, it redirects from my landing page index.php to a totally different page that displays a photo of Stonehenge-artifact1.

The problem -- if the user types www.mysite.com/index.php/ialkselasdfa
all my CSS styles disappear -- the index.php landing page special fonts and colors in my CSS stylesheet go missing.

I backed out all my mod_rewrite changes, including completely removing my custom .htaccess file in the mysite.com folder space (using a local custom copy of .htaccess is one of the steps for mod_rewrite).

I wanted to see if the CSS styles would disappear now that I was back to yesterday's (pre-mod_rewrite changes) code. Sure enough, my CSS stylesheet goes AWOL if I type an argument, ANY argument, after index.php.

So the following URLs (for example) somehow disable my CSS stylesheet for index.php

www.mysite/index.php/foo
www.mysite/index.php/css-whereAreYou

(NOTE: the reason I brought up the mod_rewrite is to explain why I'm typing something at the end of my site's URL in the first place. Were it not for me testing that new mod_rewrite code, I would not have found this problem.)

Here is my inclusion in my index.php of my CSS stylesheet:

<html>
 <head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <link rel="stylesheet" type="text/css" href="globalStyles.css" />
 </head>

My globalStyles.css is:

root { 
  display: block;
}

body
{
   color: black;
   font-family: Gill, Helvetica, sans-serif;
   font-size: 9pt;
   background-color: RGB(243,239,240); /* light blue */
} 

p
{
    color: black;    
}

Why is my index.php landing page not getting the CSS styles when I add characters after index.php?

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

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

发布评论

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

评论(4

巡山小妖精 2025-01-12 10:31:16

您还可以在 head 部分使用 标记:

<base href="base/path/for/files/to/follow">

它的作用是告诉浏览器指定的路径是根目录,并且应该请求相对于该目录的相对路径。

我在 mod_rewrite 中遇到了同样的问题(不管你信不信,这个解决方案来自 Google 搜索,尽管我必须挖掘一下)。

You could also use the <base> tag in the head section:

<base href="base/path/for/files/to/follow">

What this does is tell the browser that the path specified is the root directory, and relative paths should be requested relative to said directory.

I encountered this same issue with mod_rewrite (and, believe it or not, this solution came out of a Google search, though I had to dig a little).

野侃 2025-01-12 10:31:16

因为您使用相对路径来访问文件,所以当 URL 末尾有额外的“目录”时,它会尝试从任何目录开始,因为浏览器不知道更好(您重写了请求路径) )。也尝试更改它:

<link rel="stylesheet" type="text/css" href="/globalStyles.css" />

使用绝对路径将使其定向到 CSS 文件的确切位置,无论您的 URL 是什么。

例如:/index.php/foo实际上会将index.php视为目录,然后将foo视为index.php 的子目录,使浏览器尝试在以下位置查找 CSS 文件的路径:/index.php/foo/globalStyles.css

Because you are using a relative path to access the file, so when there are extra "directories" at the end of the URL, it tries to start in whatever directory because the browser doesn't know any better (that you rewrote the request path). Try changing it too:

<link rel="stylesheet" type="text/css" href="/globalStyles.css" />

Using an absolute path will keep it directing to the exact location of your CSS file, no matter what your URL might be.

For example: /index.php/foo will actually treat index.php as a directory and then foo as a sub-directory of index.php, making the path the browser tries to find your CSS file at: /index.php/foo/globalStyles.css.

故笙诉离歌 2025-01-12 10:31:16

使用绝对路径而不是相对路径。

Use absolute paths rather than relative paths.

许仙没带伞 2025-01-12 10:31:16

在以下页面中:

www.mysite/index.php/foo

以下标记:

<link rel="stylesheet" type="text/css" href="globalStyles.css" />

指示浏览器查找名为:的文件,

www.mysite/index.php/globalStyles.css

显然它不存在。您应该在您的情况下使用绝对路径:

<link rel="stylesheet" type="text/css" href="/globalStyles.css" />

请注意,这可能会导致另一个问题:浏览器的请求:

www.mysite/globalStyles.css

可能会被 mod_rewrite 转换/重定向为:

www.mysite/index.php/globalStyles.css

您应该调整重写规则以忽略 .css、.js、.gif (等)文件。或者,使用 RewriteCond' s -f 标志以确保该文件不存在,然后重写。

Inside the following page:

www.mysite/index.php/foo

the following tag:

<link rel="stylesheet" type="text/css" href="globalStyles.css" />

instructs the browser to look for a file called:

www.mysite/index.php/globalStyles.css

Obviously it isn't there. You should use absolute paths in your case:

<link rel="stylesheet" type="text/css" href="/globalStyles.css" />

Note that this could lead to another problem: the browser's request for:

www.mysite/globalStyles.css

might get converted/redirected by mod_rewrite to:

www.mysite/index.php/globalStyles.css

You should adjust your re-write rules to ignore .css, .js, .gif (etc) files. Alternately, use the RewriteCond's -f flag to make sure that file does not exist, then rewrite.

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