CSS、JS 和图像不显示漂亮的 url [英] CSS, JS and images do not display with pretty url

查看:28
本文介绍了CSS、JS 和图像不显示漂亮的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 htaccess 文件重写 URL,以便以下 URL

I am trying to rewrite the URL through the htaccess file so that the following URL

www.domain.com/subfolder/index.php?key

可以通过以下方式访问:

can be accessed by:

www.domain.com/subfolder/index.php/key

指定的键"将决定在 PHP 代码中包含哪个页面.我已经有了以下 htaccess 代码,但是在使用第二个(干净的)URL 时不会显示 CSS、JS、图像等.关于可能是什么问题的任何想法?

the specified "key" will determine which page to include in the PHP code. I have the following htaccess code already, however the CSS, JS, images and such are not being displayed when using the second (clean) URL. Any ideas as to what could be the issue?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
RewriteRule ^index.php/([a-zA-Z0-9/_]+)$ index.php?key=$1

推荐答案

当您使用相对 url 时,浏览器将使用它加载的资源的 url 动态创建一个完整的 url.换句话说:它使用显示在地址栏中的 url.在您的情况下 (www.domain.com/subfolder/index.php/key) 它尝试加载任何相对于 www.domain.com/subfolder/index.php/.但是,您的资源不在那里.

When you use relative url's, the browser will dynamically create a complete url by using the url of the resource it loaded. In other words: It uses the url as it is displayed in the address bar. In your case (www.domain.com/subfolder/index.php/key) it tries to load any relative url relative to www.domain.com/subfolder/index.php/. Your resources are however not located there.

您有两种方法可以解决此问题:

You have two options to resolve this problem:

  • 将您的相对 url 转换为绝对 url,至少绝对到域根.像 <img src="img/unicorn.png"> 应该变成 <img src="/path/to/img/unicorn.png">.

为您的 head 元素添加一个基础.将使用此基数代替资源的 url 来计算完整的 url.您应该将 添加到您的 元素./ 然后将用作任何相对 url 的基础.

Add a base to your head element. This base will be used instead of the url of the resource to calculate the complete url. You should add <base href="/"> to your <head> element. / will then be used as the base of any relative url.

这篇关于CSS、JS 和图像不显示漂亮的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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