CSS,JS和图像不显示与pretty的网址 [英] CSS, JS and images do not display with pretty url

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

问题描述

我想通过htaccess文件重写URL,以便以下网址

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

可以通过访问:

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

指定的钥匙将确定在PHP code中包含哪些页面。 我有以下的htaccess code不已,但是CSS,JS,图像,使用第二(清洁)URL时,这种不被显示。任何想法可能是什么问题?

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-l
的RewriteCond%{DOCUMENT_ROOT} / $ 1 -F
重写规则^ [^ /] + /([^] + \(?: JS |。CSS | JPE G |?PNG | GIF))/ $ 1 [L,R = 301,NC]
重写规则^的index.php /([A-ZA-Z0-9 / _] +)$的index.php?关键= $ 1
 

解决方案

当您使用相对网址,浏览器会动态地使用它加载的资源的URL创建一个完整的URL。换句话说:它使用的URL,因为它显示在地址栏。在你的情况下( www.domain.com/subfolder/index.php/key ),它会尝试加载任何相对相对于 www.domain URL。 COM /子文件夹/的index.php / 。你的资源但没有设在那里。

您有两个选项来解决此问题:

  • 转换你的相对URL的成绝对URL的,至少绝对域根。像< IMG SRC =IMG / unicorn.png> 应转为< IMG SRC =/路径/到/ IMG / unicorn.png>

  • 添加一个基地,你的头元素。这个基地将用来代替资源的URL来计算完整网址。您应该添加<基地的HREF =/> < HEAD> 元素。 / 将被用作任何相对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

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

解决方案

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:

  • Convert your relative url's into absolute url's, at least absolute to the domain root. Something like <img src="img/unicorn.png"> should be turned into <img src="/path/to/img/unicorn.png">.

  • 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和图像不显示与pretty的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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