htaccess mod_rewrite css/js/images访问问题 [英] htaccess mod_rewrite css/js/images access issue

查看:56
本文介绍了htaccess mod_rewrite css/js/images访问问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apache mod_rewrite,htaccess.

I am using apache mod_rewrite, htaccess.

我找到了一种方法来实现对images/css/js文件的访问,而无需从某个站点(我忘记了该站点)在html/pages中编写完整的url路径.

I find a way to achieve images/css/js file access without writing full url path in html/pages, from some site,(i forgot the site).

所以,我用htaccess编写:

So, I write in htaccess:

RewriteRule images/(.+)?$ images/$1 [NC,L]     # it take images from images folder
RewriteRule js/(.+)?$ js/$1 [NC,L]             # it take js files from js folder
RewriteRule topnav/(.+)?$ topnav/$1 [NC,L]     # it take js/css from topnav folder
RewriteRule common-style.css common-style.css # it take css from root folder
RewriteRule jquery.js jquery.js    # it take jquery from root folder

因此,所有js,css访问权限.

so, all js,css access.

但是现在我添加了另一个规则

but now i added another rule

RewriteRule script/js/(.+)?$ script/$1 [NC,L] # it should take js from script/js

它不起作用.

我知道现在目录结构是两级的,即script/js/,因此它无法解析"js"文件夹中的文件.但不知道如何处理.

i understand that now directory structure is two level i.e. script/js/ so it could not resolve file in "js" folder. but not know how to handle this.

我尝试了很多,使用了不同的模式,但是失败了.

I tried a lot, using different pattern, but faild.

再次,我的问题是访问:

Once again, My question is to access :

1) script/js/anyfile.js
2) script/css/anycssfile.css
3) script/blue_theme/Anyimagesfile.jpg
4) script/css/jquery/images/anyimages.jpg

目录结构如下:


-script
        -js
        -css
           -jquery
                   -images       
        -blue_theme

此外,获取这些资源的路径由.httaccess评估:

Also, the path to fetch these resources is evaluated as by .httaccess:

http://localhost/site/Place-an-ad/Cars/Mazda/mazda-y/mazda-y-2/script/js/anyjsfile.js

但实际上是. http://localhost/site/script/js/anyjsfile.js

推荐答案

您的模式仅匹配以给定模式(模式以$结尾)或仅包含给定模式(无$)的路径.问题是模式js/(.+)?$也将匹配script/js/,依此类推.

Your patterns do only match paths that either end with the given pattern (pattern ends with $) or just contains the given pattern (no $). And the problem is that the pattern js/(.+)?$ will also match script/js/ and so on.

通过使用^$提供路径的开始和结尾来使模式更具体:

Make your pattern more specific by providing both the start and the end of the path by using ^ and $:

RewriteRule ^images/(.+)?$ images/$1 [NC,L]
RewriteRule ^js/(.+)?$ js/$1 [NC,L]
RewriteRule ^topnav/(.+)?$ topnav/$1 [NC,L]
RewriteRule ^common-style\.css$ common-style.css
RewriteRule ^jquery\.js$ jquery.js

然后对新规则执行相同操作:

And then do the same with your new rule:

RewriteRule ^script/js/(.+)?$ script/$1 [NC,L]

这篇关于htaccess mod_rewrite css/js/images访问问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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