seo 友好 url 使用 .htaccess css 文件未加载

seo friendly url using .htaccess css file is not loaded

我有一个 url 看起来像

https://sparkle.000webhostapp.com/movie.php?id=16

我想把它改成

https://sparkle.000webhostapp.com/movie/16

我已将 .htaccess 文件创建为

   Options -MultiViews
RewriteEngine On

# redirect "/movie.php?id=xxx" to "/movie/xxx"
RewriteCond %{THE_REQUEST} \s/movie\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /movie/%1? [R=301,L]

# internally rewrite "/movie/xxx" to "/movie.php?id=xxx"
RewriteRule ^movie/([0-9]+)$ /movie.php?id= [L]

但问题是样式文件,即 css 文件未加载。 link

中没有定义样式

如果您的 CSS 是这样引用的:<link rel="stylesheet" href="homepage.css" />,它将相对于请求的路径!

最初,对 /movie.php?id=123 的请求将呈现一个页面,其 css 假定位于 /homepage.css

但是,当您创建重写时,请求被理解为 /movie/123,并且 css 被假定为 /movie/homepage.css

要解决此问题,请以 / 开始对 CSS 的引用,以便它始终引用网络根目录。

更新后的 <link> 标签应该是 <link rel="stylesheet" href="/homepage.css" />

您可以通过检查浏览器的错误控制台并查找类似“GET https://sparkle.000webhostapp.com/movie/homepage.css 404

的内容来确认这一点