php - URL 重写破坏了到 css 的链接

标签 php html css .htaccess url-rewriting

我正在使用以下设置进行 url 重写:

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 中的样式表的链接和页面显示正确。 我 fi 浏览 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

最佳答案

注释不允许我格式化代码,你能试试这个吗:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(css|jpe?g|gif|png|js|ico)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

更新

您可以在 <head> 中尝试类似的操作页面的一部分以支持该页面上引用的图像/css/js 文件的相对路径:

<head>
<base href="http://www.example.com/static-files/" />
</head>

关于php - URL 重写破坏了到 css 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5462897/

相关文章:

php - 使用 jQuery 和 SQL 从表中删除数据

PHP MySQL - 使用 LOAD DATA INFILE 导入 CSV - 确定文件路径?

html - CSS 显示表格忽略宽度

javascript - 刷新 div 容器中的广告横幅

html - 如何使导航宽度自动,同时绝对定位其包装器

javascript - 从下拉菜单中打开弹出窗口

javascript - 浏览器抗锯齿(平滑线条)

php - 通过ajax返回javascript但它不起作用

php - 如何在浏览器上打印Select查询结果?不显示从数据库获取的结果

html - div 内容边距推送容器