URL 重写:css、js 和图像未加载 [英] URL rewriting : css, js, and images not loading

查看:33
本文介绍了URL 重写:css、js 和图像未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 .htaccess

Options +FollowSymLinks

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteRule ^detail/([0-9]+)/?$ detail.php?id=$1

它将http://localhost/detail/123 URL 重定向到http://localhost/detail.php?id=123.页面重定向成功,但问题是CSS、JS和图像没有加载,

It redirects http://localhost/detail/123 URL to http://localhost/detail.php?id=123. The page redirects successfully, but the problem is CSS, JS, and images are not loading,

CSS、js 文件位于 http://localhost/css/http://localhost/js/

CSS, js files are located under http://localhost/css/ and http://localhost/js/

一种解决方案是使用绝对路径(例如/CSS 或/js 而不仅仅是 CSS/、/js 但这似乎不是一个可靠的解决方案,因为我们必须对其进行更改文件,

One solution is to use absolute path (ex /CSS, or /js rather than just CSS/, /js but this does not seem a reliable solution since we've to change it on all files,

任何其他基于 .htaccess 规则的解决方案,独立于编辑所有 PHP 文件并让我们使用相对路径"?

Any other solution based on .htaccess rules, which is independent of editing all PHP files and let us use "relative paths"?

推荐答案

不要偷懒,将资源中的相对 URI 更改为根目录绝对路径,例如 /css/style.css.这是最好的.

Don't be lazy and change your relative URIs in your resources to root directory absolute pathing like /css/style.css. This is the best.

您可以变得聪明并使用正则表达式并替换您需要的所有文件,就像几个衬里一样,您就完成了.有多少地方可以改变?您应该使用模板.

You can get clever and use regex and replace all the files you need which would be like a few one liners and you're done. How many places could there be to change? And you should be using a template.

这应该可行,但我不会这样做.

This should work but I wouldn't go this way.

RewriteRule ^detail/(css|js|img)/(.*)?$/$1/$2 [L,QSA,R=301]

这篇关于URL 重写:css、js 和图像未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆