我可以对包含的外部 javascript 文件使用 301 重定向吗?
我正在尝试清理网站上的一些文件,其中一项任务是将所有对 jquery 的引用整理到一个文件中。
是的,这是一个拥有多个开发人员的大型网站,并且没有遵循一些标准,导致目前引用了各种版本的 jquery 的情况。
我尝试做的是为这些文件创建 301 重定向以指向单个版本。
例如: 最终应该指向
/someurl/ js/jquery-core.min.js
我尝试这样做,但似乎无法加载新文件并且 jquery 不存在,我的网络面板显示原始文件上有一个 301,我可以看到参考新的,但是“响应”选项卡是空的。
是否可以通过这种方式使用 301 重定向?
感谢您的任何建议/反馈
p.s 我知道有更好的方法来引用 jquery 等,但大公司流程和繁文缛节阻碍了我以任何其他方式这样做
I am trying to clean up some files on a website, one task being to collate all references to jquery to a singular file.
Yes, it's a large site with multiple developers and some standards have not been followed resulting in the current situation where there are various versions of jquery referenced.
What I have tried to do is create a 301 redirect for these files to point to a single version.
eg: <script type="text/javascript" src="/someurl/js/jquery-1.4.4.min.js">
should end up pointing to /someurl/js/jquery-core.min.js
I have tried to do this but it appears to fail to load the new file and jquery does not exist, my net panel shows that the original file has a 301 on it and I can see the reference to the new one, however the "response" tab is empty.
Is it possible to use a 301 redirect in this way?
Thanks for any suggestions / feedback
p.s I know there are better ways to reference jquery etc but large company process and red tape stand in my way from doing this any other way
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当浏览器从 src 属性加载脚本时,它应该遵循所有重定向链接,采用与检索 html、图像、样式表等相同的方法。因此,您提供的用例应该有效。
但由于它不适合您,因此您有几种选择来解决您的问题。
使用 fiddler 或类似的调试代理来查看浏览器和服务器之间发生的情况。也许 301 格式错误,或者 mime 类型配置错误,可能是多种原因。解决该问题的方法与解决浏览器不遵循重定向的任何其他问题的方法相同。
或者...您可以使用
mod_rewrite
(或类似的服务器端 URL 重写工具)将特定版本脚本的请求修改为规范版本,而不是使用重定向。When a browser loads the script from the src attribute, it should follow all redirection links, in the same method it retrieves html, images, stylesheets, etc. So the use case you've provided should work.
But since it's not working for you, you have got a couple options to resolve your problem.
Use fiddler or a similar debugging proxy to see what's going on between your browser and the server. Perhaps the 301 is malformed, or perhaps the mime-type is misconfigured, it could be any number of things. Troubleshoot it the same way you'd troubleshoot any other issue where the browser isn't following redirects.
Or... instead of using redirects, you can use
mod_rewrite
(or a similar server-side URL rewriting tool) to modify the request for a particular version of a script to your canonical version.