URL 重写破坏了 CSS 链接

2024-04-21

我使用以下设置进行网址重写:

RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

In 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。

我怎样才能解决这个问题?

EDIT

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]

Update

你可以尝试这样的事情<head>页面的一部分,以支持该页面上引用的图像/css/js 文件的相对路径:

<head>
<base href="http://www.example.com/static-files/" />
</head>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

URL 重写破坏了 CSS 链接 的相关文章

随机推荐