我正在使用apache mod_rewrite,htaccess。
我找到了一种方法来实现images / css / js文件访问,而无需在某些网站的html / pages中写入完整的url路径,(我忘记了网站)。
所以,我写的是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访问。
但现在我添加了另一条规则
RewriteRule script/js/(.+)?$ script/$1 [NC,L] # it should take js from script/js
它不起作用。
据我所知,现在目录结构是两级,即脚本/ js /所以它无法解析“js”文件夹中的文件。但不知道如何处理这个。
我尝试了很多,使用不同的模式,但发誓。
再一次,我的问题是访问:
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评估:
http://localhost/site/Place-an-ad/Cars/Mazda/mazda-y/mazda-y-2/script/js/anyjsfile.js
答案 0 :(得分:3)
您的模式仅匹配以给定模式结尾的路径(模式以$
结尾)或仅包含给定模式(无$
)。问题是模式js/(.+)?$
也会匹配script/js/
等等。
使用^
和$
提供路径的开头和结尾,使您的模式更具针对性:
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
然后对你的新规则做同样的事情:
RewriteRule ^script/js/(.+)?$ script/$1 [NC,L]