XAMPP 中的根路径
我遇到这个问题已经有一段时间了,但没有成功地寻找答案.
I've had this problem for a while, and have unsuccessfully searched far and wide for an answer.
<img src="/images/test.jpg" />
从(根路径 - 在我的 LAMP 生产中)获取图像
Gets an image from (root path - in my case in production in LAMP)
htdocs/images/test.jpg
无论是从 htdocs/index.php 还是 htdocs/foo/bar/index.php 调用
Whether it's called from htdocs/index.php or htdocs/foo/bar/index.php
我在开发中使用 XAMPP,并且在 htdocs 内部有项目文件夹,因此上述方法虽然在现场需要我将其更改为:
I use XAMPP in development, and inside htdocs have project folders, so the method described above although will work when live requires me to alter it to:
<img src="/projectName/images/test.jpg" />
在本地工作时.
为了更简单,我定义了一个常量 BASE,我在开发中使用它:
To make this simpler i define a constant BASE, which in development I use:
define('BASE','/projectname/)
然后当它上线时,我改为:
And then when it's live I change to:
define('BASE','/')
<img src="<?php echo BASE;?>images/test.jpg" />
这显然很烦人,最终导致了几个问题.请有人对这种情况有所了解,我特别想做的是在我的图像/脚本源中使用根路径,但是:
This is obviously really annoying and ends up causing several issues. Please can someone shed some light on this situation, what I'm specifically looking to do is use root path in my image/script sources but for:
<img src="/images/test.jpg" />
当从 htdocs/projectName/foo/test.php 调用时
when called from htdocs/projectName/foo/test.php
在以下位置查找图像:
htdocs/projectName/images/
这可能吗?
推荐答案
另一种处理方法是使用虚拟主机.
An alternate way to handle this is with the use of Virtual Hosts.
虚拟主机的作用类似于专门用于子文件夹的 localhost 的第二个版本.例如,您可以设置 Apache,以便当您访问 http://example
(没有 .com
或其他任何内容)时,它会显示来自 http 的内容://localhost/example/
.所有 CSS 和 JavaScript 以及链接都将像在网站的根文件夹中操作一样,因为前面的 example
文件夹已从 URL 中删除.
A virtual host acts like a second version of localhost that works specifically for a subfolder. For example, you could set up Apache so that when you visit http://example
(no .com
or anything), it shows you the content from http://localhost/example/
. All CSS and JavaScript and links would act as if they were operating from the root folder of a website, since the leading example
folder has been trimmed out of the URL.
我找不到以前用于 XAMPP 的 演练,但在这里一个类似的,涵盖所有要点.它是为 Windows 编写的,但我想您可以为 LAMP 使用类似的机制:
I can't find a walkthrough that I used to use for XAMPP, but here a similar one that covers all of the main points. It was written for Windows, but I imagine that there are similar mechanisms that you can use for LAMP:
总而言之,以下是本文告诉您的操作:
To summarize, here's what the article tells you to do:
- 在 Apache 中启用虚拟主机
- 进行设置,以便当您访问
example
时,您将被发送到127.0.0.1
- 配置 Apache,以便当有人访问
127.0.0.1
(但网站名称为example
)时,显示来自example的内容代码>文件夹.
- Enable Virtual Hosts within Apache
- Set it up so that when you visit
example
, you are sent to127.0.0.1
- Configure Apache so that when someone visits
127.0.0.1
(but the name of the website isexample
), then it shows content from theexample
folder.
这就是您的生产站点(即具有多个网站的单个服务器)如何为每个网站设置不同的根".
This is how your production site (which is a single server with multiple websites) has a different "root" for each website.
相关文章