将 URL 中的反斜杠替换为正斜杠
我们有一个基于 Moodle 平台的系统,其中文件的引用如下:
http://[server]/file.php/3/LR4/info/index.html
现在,这很好用,但是我们的一些老师错误使用了反斜杠然后他们组织了它们的部分,这意味着某些页面是这样引用的:
http://[server]/file.php/3/LR4\info\index.html
在我们使用 Windows 时工作正常服务器+IIS。然而,后来我们改用Linux+Apache,Apache似乎不能正确处理它们。换句话说,它不显示页面上的图像,因为它们的 url 是相对的,并且如果基本 url 包含反斜杠,Apache 无法重建完整的 URL。所以页面只有文字,没有图片。
如果我手动替换反斜杠以直接输入浏览器地址行中的 URL,一切仍然正常。然而,我们有太多这样的页面。我们需要一些东西来重写 URL。我想到了 Apache mod 重写。我基本上需要的是三行代码:
RewriteEngine on
RewriteCond %{THE_REQUEST} [???]
RewriteRule ??? [R]
我需要的是 RewriteCond 的正则表达式,它将使引擎仅对包含短语 file.php 及其后的一些反斜杠的 URL 做出反应。对于 RewriteRule,我需要一些东西来用直接替换至少一个反斜杠(保持其他所有内容不变),然后进行重定向。但我似乎无法构建正确的表达式 - 有人可以帮忙吗?
we have a system, based on Moodle platform, where files are referenced like this:
http://[server]/file.php/3/LR4/info/index.html
Now, this works great, however some of our teachers by mistake used backward slashes then they organized their parts, that means, that some pages are referenced like this:
http://[server]/file.php/3/LR4\info\index.html
That worked fine while we were using Windows Server+IIS. However, then we switched to Linux+Apache, Apache does not seem to handle them correctly. In other words, it does not display images placed on the page, since their urls are relative, and Apache cannot reconstruct the full URL, if the base url contains backward slashes. So the page is just text, no pictures.
If I replace manually the backward slashes to direct in URL in browser address line, everything still works fine. However, we have too many pages like that. We need something to rewrite URL. I thought of Apache mod rewrite. What I basically need is three lines in code:
RewriteEngine on
RewriteCond %{THE_REQUEST} [???]
RewriteRule ??? [R]
What I need is the regular expression for RewriteCond that would make the engine react just for the URLs that contain phrase file.php and some backward slashes after that. And for the RewriteRule I need something that would replace at least one backward slash with direct (leaving everything else untouched) and make a redirect after that. But I can't seem to construct the correct expressions - can anyone help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您决定通过 .htaccess 执行某些操作,那么以下操作应该可行。
另一种选择是编辑 file.php 脚本并将 PATH_INFO 或 REQUEST_URI 变量中的所有“\”转换为“/”,或者由它决定要显示的文件。
If you're set on doing something through .htaccess then the following should work.
Another option would be to edit the file.php script and translate all '\' to '/' in the PATH_INFO or REQUEST_URI variables or however it determines which file to display.
尝试这样的操作:
找到此解决方案此处 - 尚未测试我。不确定是否需要这四个反斜杠(或者两个就可以)。
Try something like this:
Found this solution here - haven't tested it myself. Not sure if those four backslashes are needed (or two will do).