mod_rewrite的不加载CSS和JS文件 [英] mod_rewrite not loading css and js files

查看:173
本文介绍了mod_rewrite的不加载CSS和JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个mod_rewrite的条件/规则一个奇怪的问题。

这是我使用的条件和规则。

  RewriteEngine叙述在的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{} REQUEST_FILENAME!-l重写规则^(。*)$的index.php?PARAMS = $ 1 [QSA,L]

我的问题是,如果我访问www.example.com和www.example.com/page~~V它加载正常,但当我尝试加载www.example.com/page/category它加载的页面,但无法找到的CSS和JavaScript的。它试图从页/ CSS,而不是/目录加载它们。

这是我做了什么错有什么想法?我对这里的搜索,并试图像加了一些东西。

 的RewriteCond $ 1 \\(JS | ICO | GIF | JPG | PNG | CSS |全文|瑞士法郎| MP3 | WAV | TXT)!$

 重写规则^(图片| CSS |的JavaScript)(。*)?$的index.php PARAMS = $ 1 [QSA,L]

但是,这似乎并没有太大的差别。

感谢您阅读


解决方案

的问题是,一个无声的重写不会告诉访问者的网络浏览器,他们实际上是在不同的目录路径看一个页面。

因此​​,对于/页面的请求离开浏览器(正确地)认为它正在寻找在资源是在Web服务器上的最上层的公共目录,因为是图像和CSS文件

但对于/页/类别的请求离开浏览器(错误地)以为它是寻找一个资源这是被称为网页的子目录。因此,当浏览器发送的CSS和图像文件的请求,它会要求服务器寻找他们在/页/分目录,这是不存在的。因此,服务器当浏览器尝试获取的图像和CSS文件返回404 codeS。

来解决这个问题的一种方法是在路径的CSS,图像和其它页面引用的资源更改为绝对路径。用斜杠启动它们,使它们相对于实际的顶级公共目录挂靠。这可能是继续,如果你只有少量的资源路径更新的最佳方法。例如,更改:

<$ P为$ P> SRC =图像name.png

  SRC =/图像name.png

这将迫使基于浏览器的相对路径使用绝对路径请求文件,而不是推断错误的位置。

要删除需要一个RewriteCond指令,您可以将重写规则更改为以下内容:

 重写规则^([A-Z] +(?:/ [A-Z] +)*)?$的index.php PARAMS = $ 1 [QSA,L]

这不会匹配任何包含一个句点符号,所以对于图像和CSS文件的请求将被跳过。

I have a weird problem with a mod_rewrite condition / rule.

This is the condition and rule I'm using.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php?params=$1 [QSA,L]

My problem is if I visit www.example.com and www.example.com/page it loads fine but when I try to load www.example.com/page/category it loads the page but cannot find the css and javascript. It tries to load them from page/css instead of the / directory.

Any thoughts on what I've done wrong? I had a search on here and tried a few things like add

RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav|txt)$ 

and

RewriteRule ^(images|css|javascript)(.*)$ index.php?params=$1 [QSA,L]

But that didn't seem to make much difference.

Thanks for reading

解决方案

The problem is that a silent rewrite will not tell the visitor's web browser that they are in fact looking at a page from a different directory path.

So a request for /page leaves the browser (correctly) thinking that it is looking at a resource which is in the top-level public directory on the web server, as are the images and CSS files.

But a request for /page/category leaves the browser (wrongly) thinking that it is looking at a resource which is in a sub-directory called page. So when the browser sends requests for the CSS and image files, it will ask the server to look for them in the /page/ sub-directory, which does not really exist. So the server returns 404 codes when the browser tries to fetch image and CSS files.

One way to get around this is to change the paths to CSS, image and other page-referenced resources to absolute paths. Start them with a forward-slash so that they are anchored relative to the actual top-level public directory. This is probably the best way to proceed if you only have a small number of resource paths to update. For example, change:

src="image-name.png"

into

src="/image-name.png"

This will force the browser to request the file using an absolute path rather than deduce the wrong location based on a relative path.

To remove the need for a RewriteCond, you can change your RewriteRule to the following:

RewriteRule ^([a-z]+(?:/[a-z]+)*)$ index.php?params=$1 [QSA,L]

This will not match anything which contains a period (dot) symbol, so requests for image and CSS files will be skipped.

这篇关于mod_rewrite的不加载CSS和JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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