URL重写打破了链接的CSS [英] Url rewriting broke link to css

查看:307
本文介绍了URL重写打破了链接的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解决您的问题尚未...):

  1. 正在重写所有的文件名,所以当浏览器请求 CSS / index.css 的重写转变,为指数。 PHP?URL = CSS / index.css 。 @ cOle2的答案应该解决的。

  2. 您没有使用绝对路径为你的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是不是由浏览器访问的,所以你需要:

  1. 将在CSS的项目/公目录(如项目/公/ CSS
  2. 使用绝对路径,以你的CSS像 /css/index.css
  3. 确保该网址到外部文件不被服务器重写。

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...):

  1. You are rewriting all file-names so when the browser is requesting css/index.css your rewrite transforms that to index.php?url=css/index.css. @cOle2's answer should solve that.

  2. 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 like css/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:

  1. Move the css to the project/public directory (like project/public/css)
  2. Use absolute paths to your css like /css/index.css.
  3. Make sure the urls to your external files are not rewritten by the server.

这篇关于URL重写打破了链接的CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆