Why does .htaccess redirect URL routing fine but break css/js/image links after two levels deep?

家住魔仙堡 提交于 2019-12-03 21:38:31

From the absolute URL suggestions, if the files are always at the root of the domain, and if you're in control of the html, you could do the leading-slash absolute, which might avoid having to edit them among servers.

/css/main.css
/js/main.js
/images/test.jpg

htaccess rules can help if the files move, or you're not in control of the html. e.g., I work on project where some are using dreamweaver, which loves to do ../images/test.jpg.

RewriteRule (css|js|images)/(.+)$ /$1/$2 [NC,QSA,L]

This assumes the files are actually at the root of the domain. If that varies among servers, then you'll need to edit the paths in htaccess - but that's still easier than editing the html.

This assumes all the style, script and images are in the three folders css, js, images; and these are the only folders with these names. e.g., there can't be another images folder in some other subfolder.

This will break any folder ending in these strings, e.g., myimages or somecss. That can be avoided by tweaking the rule to require these to stand alone. Then those folders won't be changed, only the specific css, js, and images folders.

RewriteRule (?:^|/)(css|js|images)/(.+)$ /$1/$2 [NC,QSA,L]

The (?:^|/) means non-capturing ?:, at the beginning ^ or after a slash /.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!