如何重写某些页面而不是 CSS,JS 等?

How to rewrite certain pages but not CSS, JS etc?

我已经尝试了至少 4 个小时来自行解决这个问题,并且已经搜索了数百个 Stack Overflow 页面和其他论坛以寻找解决方案,但我似乎无法解决这个问题简单的问题。

内容安排如下:

-- index.php
  -- js
       - script.js
  -- css
       - style.css
  -- images
       - image.png

在网站中,我需要index.php?dept=foo改写为dept/foo。这适用于以下重写规则:

RewriteRule ^dept/(\w+)/?$ index.php?dept= [NC]

但是 CSS、JS 和图像将不再加载。

CSS目前包含的JS文件如下:

<link rel="stylesheet" href="css/style.css" />

我尝试将 URL 更改为具有绝对路径(也就是 /css/style.css`,但这也没有解决问题)。

如何正确解决问题?

根据您的评论有此重写规则:

RewriteEngine On

RewriteRule ^dept/(\w+)/?$ index.php?dept= [NC,L,QSA]

然后将其添加到您页面 HTML 的 <head> 部分下方:

<base href="/live/" />

这样每个 亲戚 URL 都是从那个基础 URL 解析的,而不是从当前页面的 URL.

您可以将 CSS 路径保留为:

<link rel="stylesheet" href="css/style.css" />