URL 重写破坏了 CSS 链接

发布于 2024-10-28 02:38:19 字数 1277 浏览 3 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧瑾黎汐 2024-11-04 02:38:19

评论不允许我格式化代码,您可以尝试以下操作:

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]

更新

您可以在页面的 部分中尝试类似的操作,以支持 image/css/ 的相对路径该页面引用的 js 文件:

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

Comment doesn't let me format the code, can you please try this:

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

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:

<head>
<base href="http://www.example.com/static-files/" />
</head>
山色无中 2024-11-04 02:38:19

我认为你有两个问题(因为给定的答案还没有解决你的问题...):

  1. 你正在重写所有文件名,所以当浏览器请求 css/index.css 您的重写将其转换为 index.php?url=css/index.css。 @cOle2 的答案应该可以解决这个问题。

  2. 您没有对 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,因此您需要:

  1. 将CSS移动到project/public目录(如 project/public/css
  2. 使用 css 的绝对路径,如 /css/index.css
  3. 确保外部文件的 URL 没有被服务器重写。

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.
一个人练习一个人 2024-11-04 02:38:19

您可以尝试从重写规则中删除某些文件扩展名,例如以下规则将忽略图像、css 和 js 文件。

# Do not process images or CSS files further
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]

编辑:这就是我应用它的方式:

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

You can try removing certain file extensions from the rewrite rule, for example the following rule will disregard images, css and js files.

# Do not process images or CSS files further
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]

Edit: This is how I would apply it:

RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文