如何将请求重定向到站点根目录上方的特定文件?
我正在启动一个新网站,但在执行我想要的文件/文件夹组织方面遇到了困难:
为了论证起见,假设我的网站将托管在: http://mywebsite.com/
我希望(已设置)Apache 的虚拟主机来映射http://mywebsite.com/ 到 /fileserver/mywebsite_com/www 文件夹。
当我决定将一些文件(favicon.ico 和 robots.txt)放入 Apache 正在安装 http://mywebsite.com/ 进入
robots.txt+favicon.ico go into => /fileserver/files/mywebsite_com/stuff
因此,当人们访问 http://mywebsite.com/robots.txt,Apache 将为他们提供来自 /fileserver/mywebsite_com/stuff/robots.txt 的文件
我尝试通过 mod_rewrite 设置重定向,但唉:
RewriteRule ^(robots\.txt|favicon\.ico)$ ../stuff/$1 [L]
对我没有任何好处,因为基本上我是在告诉 apache 提供高于其安装根目录的东西。
是否可以通过以不同方式设置 Apache (2.2.9) 虚拟主机,或者定义某种 RewriteMap 来实现所需的功能,该 RewriteMap 会将有问题的 URL 重写为系统文件路径而不是其他 URL? 如果没有,所需组织(如果有)的首选行动方案是什么?
我知道我可以通过 PHP 访问前面提到的文件,然后流式传输它们 - 比如使用 readfile(..),但我想让 Apache 做尽可能多的工作 - 它肯定比执行 I/O 更快通过 PHP。
非常感谢,这已经剥夺了我数小时的建设性工作时间。 更不用说可怜的 Apache 每隔几分钟就重新启动一次。 想想可怜的阿帕奇吧:)
I'm starting up a new web-site, and I'm having difficulties enforcing my desired file/folder organization:
For argument's sake, let's say that my website will be hosted at:
http://mywebsite.com/
I'd like (have set up) Apache's Virtual Host to map http://mywebsite.com/ to the /fileserver/mywebsite_com/www folder.
The problem arises when I've decided that I'd like to put a few files (favicon.ico and robots.txt) into a folder that is ABOVE the /www that Apache is mounting the http://mywebsite.com/ into
robots.txt+favicon.ico go into => /fileserver/files/mywebsite_com/stuff
So, when people go to http://mywebsite.com/robots.txt, Apache would be serving them the file from /fileserver/mywebsite_com/stuff/robots.txt
I've tried to setup a redirection via mod_rewrite, but alas:
RewriteRule ^(robots\.txt|favicon\.ico)$ ../stuff/$1 [L]
did me no good, because basically I was telling apache to serve something that is above it's mounted root.
Is it somehow possible to achieve the desired functionality by setting up Apache's (2.2.9) Virtual Hosts differently, or defining a RewriteMap of some kind that would rewrite the URLs in question not into other URLs, but into system file paths instead?
If not, what would be the preffered course of action for the desired organization (if any)?
I know that I can access the before mentioned files via PHP and then stream them - say with readfile(..), but I'd like to have Apache do as much work as necessary - it's bound to be faster than doing I/O through PHP.
Thanks a lot, this has deprived me of hours of constructive work already. Not to mention poor Apache getting restarted every few minutes. Think of the poor Apache :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您设置为使用 RewriteRule。 不过,我建议您使用别名:
此外,您还必须告诉 Apache 该文件的限制。 如果您有多个文件以这种方式处理,请对整个目录执行此操作:
It seems you are set to using a RewriteRule. However, I suggest you use an Alias:
Additionally, you will have to tell Apache about the restrictions on that file. If you have more than one file treated this way, do it for the complete directory:
你可以使用符号链接吗?
(
ln
类似于cp
,但使用-s
创建符号链接而不是副本。)Can you use symlinks?
(
ln
is likecp
, but creates symlinks instead of copies with-s
.)