CSS,JS和图像不显示与pretty的网址图像、网址、CSS、JS

由网友(脱了缰的脂肪)分享简介:我想通过htaccess文件重写URL,以便以下网址www.domain.com/subfolder/index.php?key可以通过访问:www.domain.com/subfolder/index.php/key指定的钥匙将确定在PHP code中包含哪些页面。我有以下的htaccess code不已,但是CS...

我想通过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.

阅读全文

相关推荐

最新文章