使 URL 在本地和远程工作以进行开发

发布于 2024-12-20 22:50:12 字数 549 浏览 5 评论 0原文

我想要一个用于本地/远程 Web 开发的无缝且可移植的解决方案,它允许我使用在本地开发环境中本地工作的绝对 URL/URI,并且无需修改也可以在远程服务器上工作。

我已经让 MySQL 以这种方式工作,因为我将主机名重定向到我的 127.0.0.1机,并使用远程服务器使用的相同主机名,以及相同的用户和密码。

但是当谈到 HTTP 时,我当然不能这样做,因为这样我就无法从浏览器访问远程 URL,反过来我会看到本地 Web 服务器。

那么有办法吗?

我在 Apache HTTP Server 上使用 PHP,以及几个不同的 MVC、WordPress 等,因此通过代码执行此操作可能在项目之间不太可移植,并且还具有区分本地与远程的签入代码,可能会对生产服务器性能。

我知道如何在 PHP 中执行此操作,并且我宁愿在本地环境中进行一些配置,因此远程服务器不必为此进行额外的处理。 此外,有些文件不经过 PHP 预处理,例如 .js 文件,也需要这样做。

I want a seamless and portable solution for local/remote web development, that allows me to use absolute URLs/URIs that work locally when on my local development environment and also work on the remote server without modification.

I already got MySQL working this way, as I redirected the host name to 127.0.0.1 on my machine, and use the same host name that the remote server uses, together with same user and pass.

But when comes to HTTP, I can't of course do the same, because then I couldn't access the remote URL from the browser and in turn I would be seeing the local web server.

So is there a way?

I'm using PHP on Apache HTTP Server, and several different MVCs, WordPress, etc. so maybe doing this by code wouldn't be very portable between projects and also having a check in code that discriminates local from remote, may impact negatively on the production server performance.

I know how to do this in PHP, and I'd rather do some configuration on my local environment, so the remote server doesn't have to do extra processing for this.
Also, there are files that don't go through PHP pre-processing like .js files, that need this too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-小熊_ 2024-12-27 22:50:12

您可以使用 $_SERVER['HTTP_HOST'] 获取用于访问该页面的主机名(或 IP 地址)。因此,您可以使所有绝对 URL

$url = "http://{$_SERVER['HTTP_HOST']}/path/to/file";

代替

$url = "http://localhost/path/to/file";

此值将从 HTTP 请求的 Host: 标头中获取,因此它也会考虑非标准端口。

You can use $_SERVER['HTTP_HOST'] to get the hostname (or IP address) that was used to access the page. So you can make all your absolute URLs

$url = "http://{$_SERVER['HTTP_HOST']}/path/to/file";

Instead of

$url = "http://localhost/path/to/file";

This value will be taken from the Host: header of the HTTP request, so it will also account for non-standard ports.

音盲 2024-12-27 22:50:12

你可以有这样的东西(如何包含它取决于你):

if($_SERVER['SERVER_NAME'] == "localhost"){
        $baseurl = "http://localhost/";
}else{
        $baseurl = "http://livesite.com/";
}

然后当你想添加链接时:

<a href="<?php echo $baseurl; ?>index.html">Home</a>

You can have something like this (how you include it is up to you):

if($_SERVER['SERVER_NAME'] == "localhost"){
        $baseurl = "http://localhost/";
}else{
        $baseurl = "http://livesite.com/";
}

and then when you want to add a link:

<a href="<?php echo $baseurl; ?>index.html">Home</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文