URL 重写:css、js 和图像未加载 [英] URL rewriting : css, js, and images not loading
问题描述
我遵循 .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屋!