php URL 重写:css、js 和图像未加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12883273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:25:42  来源:igfitidea点击:

URL rewriting : css, js, and images not loading

phpapache.htaccessmod-rewriteurl-rewriting

提问by Bhavesh Gangani

I've following rule for .htaccess

我遵循规则 .htaccess

Options +FollowSymLinks

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

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

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

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

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

CSS、js 文件位于http://localhost/css/http://localhost/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,

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

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

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

采纳答案by Anthony Hatzopoulos

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.

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

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]

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

回答by Jon Lin

one solution is that use absolute path (ex /css, or /js rather than just css/, /js but this is not looks a reliable solution since we've to change it on all files,

一种解决方案是使用绝对路径(例如 /css 或 /js 而不仅仅是 css/、/js 但这看起来不是一个可靠的解决方案,因为我们必须在所有文件上更改它,

This is because your relative URIs have their base changed. Originally, the base is /when the page is /detail.php?id=123, and the browser properly fills in relative links with the /base. But when the browser goes to a page like /detail/123the base suddenly becomes /detail/and it tries to append that in front of all relative URLs and thus none of them load.

这是因为您的相对 URI 的基址已更改。原来base是/页面所在的时候/detail.php?id=123,浏览器正确填写与/base相关的链接。但是当浏览器进入一个像/detail/123base这样的页面时/detail/,它会尝试将它附加到所有相对 URL 的前面,因此它们都没有加载。

You can either make your links absolute, or change the URI base in the header of your pages (inbetween the <head> </head>tags):

您可以将链接设为绝对链接,也可以更改页面标题中的 URI 基础(<head> </head>标签之间):

<base href="/">

回答by Sajal

I applied url rewriting to a project in core php. I faced the same problem. css, Js and images were not getting loaded. I used this and it worked for me. Source

我将 url 重写应用于核心 php 中的一个项目。我遇到了同样的问题。css、Js 和图像未加载。我使用了这个,它对我有用。来源

# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

回答by Perleone

Anthony's answer is perfectly valid, and he is actually trying to impart some good practices there.

Anthony 的回答是完全有效的,他实际上是在尝试在那里传授一些好的做法。

However, since you do not seem content with that answer, here is a different solution. Just insert this line into your .htaccess before any Rewrite-Command:

但是,由于您似乎对该答案不满意,因此这里有一个不同的解决方案。只需在任何重写命令之前将此行插入到您的 .htaccess 中:

RedirectMatch permanent ^/detail/((css|js)/.*)  /

It will redirect all the originally 404 requests for /details/css/style.css and /details/js/js.s to their actual location. Neither particluarly pretty nor elegant, but it simply works.

它会将 /details/css/style.css 和 /details/js/js.s 的所有原始 404 请求重定向到它们的实际位置。既不特别漂亮也不优雅,但它只是有效。

回答by danu

this code perfectly work for me, add before redirect rule(RewriteRule ^detail/([0-9]+)/?$ detail.php?id=$1)

这段代码非常适合我,在重定向规则之前添加(RewriteRule ^detail/([0-9]+)/?$ detail.php?id=$1)

RewriteRule ^(.+)/(admin|css|fonts|ico|include|js|images)/(.*)$ / [L]