URL 重写破坏了 CSS 链接
我使用以下设置进行网址重写:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
在 index.php
中解析 $_GET['url']
,以便在以下示例中:
ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby
ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome
因此第一个参数(?我不知道如何称呼它)是页面名称,然后以下几个参数就像“键/值”。 现在,当我浏览 ROOT/
时,插入到页面 html 内的样式表的链接和页面会正确显示。 我浏览 ROOT/index
(与 ROOT/
相同),它正确显示页面(包含内容和其他内容),但链接(即使在 html 中)结构已正确写入)到样式表未加载。我可以看到,当我加载页面时,我的页面根本没有 css。
我该如何解决这个问题?
编辑
css文件的路径如下:
project/view/css/common.css
它包含的文件位于
project/public/index.php // the one with .htaccess and rewrite rules
这使我可以创建一个链接(在index.php内),例如
../view/css/common.css
但是这根据url的显示方式而有所不同。例如:
# For URL = public/
project/view/css/common.css // good
# For URL = public/index/
project/public/view/css/common.css // broken
# For URL = public/index/key/value
project/public/index/key/view/css/common.css // broken
I'm using the following setting for url rewriting:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
In index.php
is parse $_GET['url']
so that in the following examples:
ROOT/user/id/1/name/bobby // user is the page, id = 1, name = bobby
ROOT/blog/post/123/title/welcome // blog is the page, post = 123, title = welcome
So that the first parameter(? i don't know how to call it) is the page name then the following couple of parameters are like "keys/value".
Now when i browse ROOT/
the link to stylesheets that are inserted inside the html of the page and the page are shown correctly.
I fi browse ROOT/index
(which is the same as ROOT/
) it shows the page (with contents and other stuff) correctly but the links (even if in the html structure are correctly written) to stylesheets are not loaded. And i can see that from the fact that my page has no css at all when i load it.
How can I fix this?
EDIT
The css file's path is as follows:
project/view/css/common.css
The file where is it included is in
project/public/index.php // the one with .htaccess and rewrite rules
This brings me to make a link (inside the index.php) such as
../view/css/common.css
But this works different depending on how the url seems. For examples:
# For URL = public/
project/view/css/common.css // good
# For URL = public/index/
project/public/view/css/common.css // broken
# For URL = public/index/key/value
project/public/index/key/view/css/common.css // broken
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
评论不允许我格式化代码,您可以尝试以下操作:
更新
您可以在页面的
部分中尝试类似的操作,以支持 image/css/ 的相对路径该页面引用的 js 文件:
Comment doesn't let me format the code, can you please try this:
Update
You can try something like this in the
<head>
section of your pages to support relative path for your image/css/js files referenced on that page:我认为你有两个问题(因为给定的答案还没有解决你的问题...):
你正在重写所有文件名,所以当浏览器请求
css/index.css
您的重写将其转换为index.php?url=css/index.css
。 @cOle2 的答案应该可以解决这个问题。您没有对 css 文件使用绝对路径。服务器正在将
/user/id/1/name/bobby
等请求的页面转换为/index.php?etc....
但是当浏览器请求时代码中类似于css/index.css
的 css 文件,它实际上会请求/user/id/1/name/bobby/css/index.css
并且该文件不存在。您可以通过仅使用外部文件(css、js、图像等)的绝对路径来解决这个问题,例如/css/index.css
。编辑:根据您的评论,您的路径都是相对路径,并且浏览器无法访问您的CSS,因此您需要:
project/public
目录(如project/public/css
)/css/index.css
。I think you have two problems (as the given answers didn´t solve your problem yet...):
You are rewriting all file-names so when the browser is requesting
css/index.css
your rewrite transforms that toindex.php?url=css/index.css
. @cOle2's answer should solve that.You are not using absolute paths for your css files. The server is translating the requested page like
/user/id/1/name/bobby
to/index.php?etc....
but when the browser requests for example the css file that is something likecss/index.css
in your code, it will actually request/user/id/1/name/bobby/css/index.css
and that file does not exist. You can solve that by using only absolute paths to your external files (css, js, images, etc.) like/css/index.css
.Edit: Based on your comments, both your paths are relative and your css is not accessible by the browser, so you need to:
project/public
directory (likeproject/public/css
)/css/index.css
.您可以尝试从重写规则中删除某些文件扩展名,例如以下规则将忽略图像、css 和 js 文件。
编辑:这就是我应用它的方式:
You can try removing certain file extensions from the rewrite rule, for example the following rule will disregard images, css and js files.
Edit: This is how I would apply it: