确定网站的“根”地点
我需要一些有关网站“根”网址和目录的概念和术语的帮助。
是否可以确定网站的根目录,或者这是一个任意的想法,只能建立实际服务器的根目录?
假设我正在编写一个 PHP 插件,它将由不同位置的不同网站使用,但需要确定网站的基本目录是什么。使用 PHP,我将始终能够确定 DOCUMENT_ROOT 和 SERVER_NAME,即服务器(或虚拟服务器)的绝对 URL 和绝对目录路径。但我不知道网站本身是“安装”在根目录还是子目录中。如果网站位于子目录中,我需要用户显式设置“子路径”变量。正确的?
I need some help with concepts and terminology regarding website 'root' urls and directories.
Is it possible to determine a website's root, or is that an arbitrary idea, and only the actual server's root can be established?
Let's say I'm writing a PHP plugin that that will be used by different websites in different locations, but needs to determine what the website's base directory is. Using PHP, I will always be able to determine the DOCUMENT_ROOT and SERVER_NAME, that is, the absolute URL and absolute directory path of the server (or virtual server). But I can't know if the website itself is 'installed' at the root directory or in a sub directory. If the website was in a subdirectory, I would need the user to explicitly set an "sub-path" variable. Correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题 1 的回答:是的,您需要一个显式设置网站根路径的变量。可以通过每个网站根目录下包含以下行的 htaccess 文件来完成:
http://httpd.apache.org/docs/2.0/mod/mod_env.html
您可以使用以下命令在 php 脚本中的任何位置访问它:
http://php.net/manual/en/function.getenv.php
Answer to question 1: Yes you need a variable which explicitly sets the root path of the website. It can be done with an htaccess file at the root of each website containing the following line :
http://httpd.apache.org/docs/2.0/mod/mod_env.html
And you can access it anywhere in your php script by using :
http://php.net/manual/en/function.getenv.php
是
输出:
Yes
Output:
您不需要要求用户提供任何信息。
此代码片段将让您的代码知道它是否在根目录中运行:
You shouldn't need to ask the user to provide any info.
This snippet will let your code know whether it is running in the root or not:
我刚刚遇到了同样的问题。我想引用网站结构根目录中的链接和其他文件。
我尝试了以下方法,但它永远不会按照我想要的方式工作:
但明显的解决方案就是使用以下内容:
I have just had the same problem. I wanted to reference links and other files from the root directory of my website structure.
I tried the following but it would never work how I wanted it:
But the obvious solution was just to use the following: