使 URL 在本地和远程工作以进行开发
我想要一个用于本地/远程 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
$_SERVER['HTTP_HOST']
获取用于访问该页面的主机名(或 IP 地址)。因此,您可以使所有绝对 URL代替
此值将从 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 URLsInstead of
This value will be taken from the
Host:
header of the HTTP request, so it will also account for non-standard ports.你可以有这样的东西(如何包含它取决于你):
然后当你想添加链接时:
You can have something like this (how you include it is up to you):
and then when you want to add a link: