htaccess 规则破坏 css / js
我创建了一个简单的 htaccess 规则来将 /jobs/24 重写为 index.php?job=23
但由于某种原因,这似乎破坏了我的整个网站,我的 css 规则或 js 文件都不再应用。
什么会导致这个问题?
我环顾四周,尝试了一些不同的解决方案,但似乎没有任何效果
I've created a simple htaccess rule to rewrite /jobs/24 to index.php?job=23
But for some reason this seem to break my entire website, none of my css rules or js files are applied anymore.
What can cause this problem?
I've looked around a lot, trying some different solutions, but nothing seems to work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的重写规则没有任何问题。可能您只是不正确地处理您的 css/js 文件。
让我尝试用一个例子来阐明我的意思。假设你的目录结构如下所示:
现在你将那个 JS 文件包含在你的 index.php 中,
如果你直接调用 index.php 这将起作用 - 但如果你调用
jobs/24
并将其重写为 index.php 。 php,不会。重写不是重定向!客户端不知道这一点,仍然认为请求的文件位于目录jobs
中。这意味着浏览器将在以下位置查找default.js:/jobs/js/default.js
您可以使用绝对路径来避免此类问题:
There is nothing wrong with your rewrite rule. Probably your simply addressing your css/js files not correctly.
Let me try to clarify what I mean with an example. Say your directory structure looks like this:
Now your including that JS file in your index.php
This will work if you call index.php directly - but if you call
jobs/24
and rewrite that to index.php, it won't. A rewrite isn't a redirection! The client doesn't know about it and still thinks that the requested file is in a directoryjobs
. This means that the browser will look for the default.js in the following place:/jobs/js/default.js
You can make use of absolute pathes to avoid such problems:
您的文件很可能再也找不到了,因为所有内容都被重写了。您可以为所需的文件添加例外,或排除整个目录。
要排除整个目录,只需在该文件夹中创建以下
.htaccess
文件:要排除某些路径,请将其放在原始
.htaccess
之上:例如,您可以使用 Firebug 进行测试,网络选项卡显示任何无法加载的文件。
Most likely your files cannot be found anymore, because everything is being rewritten. You can add exceptions for the files you need, or exclude a whole directory.
To exclude a whole directory just create the following
.htaccess
file in that folder:To exclude certain paths put this above your original
.htaccess
:You can test this using Firebug for example, the net tab shows you any file that couldnt be loaded.
重写规则也适用于 css 和 javascript url。查看这些内容,看看它们是否与您的重写规则中的模式匹配。如果存在,请使用重写条件排除它们。 有关详细信息,请参阅文档。
The rewrite rules also apply to the css and javascript urls. Have a look at those and see if they match the pattern in your rewrite rule. If they do, exclude them using a rewrite condition. See the documentation for details.