Php 如何对本地主机和远程服务器使用相对链接而不是绝对链接

Php 如何对本地主机和远程服务器使用相对链接而不是绝对链接,php,html,web,absolute-path,rel,Php,Html,Web,Absolute Path,Rel,我的Apache web服务器根目录设置为“C:\webserver\webroot\AptanaProjects” 我希望能够引用样式表而不必输入绝对引用,因为我使用FTP上传到网站 The http://localhost/MovieDatabase/index.php file refers to http://localhost/MovieDatabase/styles/main.css using: <link rel="stylesheet" href="/styles/m

我的Apache web服务器根目录设置为“C:\webserver\webroot\AptanaProjects”

我希望能够引用样式表而不必输入绝对引用,因为我使用FTP上传到网站

The http://localhost/MovieDatabase/index.php file refers to http://localhost/MovieDatabase/styles/main.css

using:

<link rel="stylesheet" href="/styles/main.css" type="text/css"> 
http://localhost/MovieDatabase/index.php 文件引用http://localhost/MovieDatabase/styles/main.css
使用:
这是行不通的,因为显然我没有在路径中包含MovieDatabase部分

我如何解决这个问题以避免陷入绝对路径?我不想将webserver根目录更改为MovieDatabase,因为我有多个项目


谢谢。

通过删除前导斜杠使用相对路径:

<link rel="stylesheet" href="styles/main.css" type="text/css">

您的样式或脚本必须与当前文件相关

如果在任何路径之前使用斜杠,将其作为本地文件访问将通知浏览器在根路径(c:)上查找文件

在开发环境中,可以为应用程序使用Apache mod_别名(别名)。它可以根据您的服务器路径配置使用。

您可以使用:

<link rel="stylesheet" href="./styles/main.css" type="text/css"> 



或者看看RichieHindle的答案。

这是一个解决方案,但不是有一个可以根据我的需要升级localhost的解决方案吗?如果我试图在本地复制一个充满领先斜杠的网站,该怎么办?
<link rel="stylesheet" href="/MovieDatabase/styles/main.css" type="text/css">