URL重写打破了链接的CSS [英] Url rewriting broke link to css
问题描述
我用下面的设置URL重写:
RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^(。*)$的index.php?URL = $ 1 [QSA,L]
在的index.php
是解析 $ _ GET ['URL']
所以,在下面的例子:
ROOT /用户/ ID / 1 /姓名/鲍比//用户的页面,ID = 1,名称=鲍比
ROOT /博客/后/ 123 /标题/欢迎//博客的页面,后期= 123,标题=欢迎
所以,第一个参数(?我不知道该怎么称呼它)是网页的名称,然后下面的几个参数都是类似键/值。
现在,当我浏览 ROOT /
链接插入页面,该页面的HTML里的样式表正确显示。
我网络浏览 ROOT /指数
(这是一样的 ROOT /
)它显示的页面(与内容等东西),但正确的链接(即使在HTML结构是否正确写入),以样式不加载。我可以看到,从我的页面没有CSS在所有的事实,当我加载它。
我该如何解决这个问题?
修改
在CSS文件的路径如下:
项目/视图/ CSS / common.css
在这里是它包含的文件是在
项目/公/ index.php文件//一个与的.htaccess和重写规则
这使我想到做一个链接(在index.php内),如
../视图/ CSS / common.css
不过,这部作品不同,这取决于如何的URL看起来。对于例子:
#对于URL =公/
项目/视图/ CSS / common.css //好
#对于URL =公共/指数/
项目/公/视图/ CSS / common.css //破
#对于URL =公共/索引/键/值
项目/公/索引/键/视图/ CSS / common.css //破
我觉得你有两个问题(如给定的答案didn't解决您的问题尚未...):
-
正在重写所有的文件名,所以当浏览器请求
CSS / index.css
的重写转变,为指数。 PHP?URL = CSS / index.css
。 @ cOle2的答案应该解决的。 -
您没有使用绝对路径为你的CSS文件。服务器是翻译类的请求的页面
/用户/ ID / 1 /姓名/鲍比
到/index.php?etc ....
但是,当浏览器请求例如CSS文件,该文件是一样的东西CSS / index.css
在code,它实际上要求/user/id/1/name/bobby/css/index.css
键,该文件不存在。你可以解决这个仅使用绝对路径到外部文件(CSS,JS,图片等),如/css/index.css
。
编辑:根据您的意见,您的路径是相对的,你的CSS是不是由浏览器访问的,所以你需要:
- 将在CSS的
项目/公
目录(如项目/公/ CSS
) - 使用绝对路径,以你的CSS像
/css/index.css
。 - 确保该网址到外部文件不被服务器重写。
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
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:
- Move the css to the
project/public
directory (likeproject/public/css
) - Use absolute paths to your css like
/css/index.css
. - Make sure the urls to your external files are not rewritten by the server.
这篇关于URL重写打破了链接的CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!