Apache/PHP - 别名传出 URL

发布于 2024-08-26 01:55:44 字数 485 浏览 6 评论 0原文

在我的 PHP 代码中,我引用了另一个网站上的 URL。我正在本地测试我的网站(使用 XAMPP)。是否可以让 Apache 自动将其他网站的域名替换为 localhost?

例如,我的 PHP 代码可能是:

<?php echo "<a href='http://www.othersite.com'>Click me</a>"; ?>

我希望本地 Apache 将其别名为

<?php echo "<a href='http://localhost'>Click me</a>"; ?>

Is this possible?

[编辑] 只是为了澄清,我想在不更改 PHP 代码的情况下执行此操作。可能不可能,但我认为值得一问,因为这将使本地测试变得更简单,而不必修改 PHP 代码。

干杯, 担。

In my PHP code I've got references to a URL on another website. I'm testing my website locally (using XAMPP). Is it possible to get Apache to automatically replace the domain of this other website for localhost?

For example my PHP code might be:

<?php echo "<a href='http://www.othersite.com'>Click me</a>"; ?>

And I'd like for my local Apache to alias this to

<?php echo "<a href='http://localhost'>Click me</a>"; ?>

Is this possible?

[edit] Just to clarify, I want to do this without changing PHP code. Might not be possible, but thought it worth asking as it would make testing locally simpler without having to have hacks in the PHP code.

Cheers,
Dan.

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

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

发布评论

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

评论(3

北方。的韩爷 2024-09-02 01:55:44

Apache 支持虚拟主机的别名。使用 ServerAlias 指令 和只需为其他域创建一个别名即可。

您必须更新您的主机文件(您使用的是什么操作系统?)以将另一个域指向本地主机(因此它不会执行常规的旧 DNS 查找)。重新启动 Apache,您就可以开始工作了。

Apache supports aliases for virtual hosts. Use the ServerAlias directive and just create an alias for the other domain.

You'll have to update your hosts file (what OS are you using?) to point the other domain to localhost (so it doesn't do a regular old DNS lookup). Restart Apache and you should be in business.

东走西顾 2024-09-02 01:55:44

这听起来不像是 Apache 的工作;即使在 mod_rewrite 中也不行,因为事件顺序如下:

  1. 您的 PHP 脚本使用您提供的代码为页面创建 HTML,参考 www.othersite.com
  2. Apache 仅将此代码发送到用户的浏览器。
  3. 当用户单击该链接时,浏览器会尝试访问 www.othersite.com。您的 Apache 与此步骤无关。

这里您需要的是对 PHP 脚本生成的输出进行后处理。你猜怎么着:PHP 已经可以做到这一点。您可以查找一个 Apache 模块,该模块在将输出发送到客户端之前对其进行处理,但如果您使用 PHP,那么它是多余的。

Silmaril89 提出的解决方案对我来说看起来很合理;有一个变量可以告诉您是否处于“测试模式”并根据该变量构建您的 URL。

It doesn't sound like a job for Apache; not even in mod_rewrite, as the sequence of events is as follows:

  1. Your PHP script creates HTML for the page using the code you supplied, with reference to www.othersite.com
  2. Apache ONLY sends this code to the user's browser.
  3. When the user clicks on the link, the BROWSER tries to access www.othersite.com. Your Apache has nothing to do with this step.

What you need here is something to post-process the output generated by your PHP script. Guess what: PHP can already do that. You could look for an Apache module that does processing on output before sending it to the client, but it's redundant if you're using PHP.

The solution proposed by Silmaril89 looks pretty sound to me; have a variable that tells whether you're on "testing mode" and build your URLs according to that.

等你爱我 2024-09-02 01:55:44

我不确定为什么你会想做你想做的事。但是您可以创建一个变量并创建 if 语句。

if (you are localhost)
    $p = "localhost"
else 
    $p = "www.othersite.com"

<a href='http://'.$p.'>Click me</a>

从语法的角度来看,这是不正确的,但它是基本的伪代码

I'm not sure why you would ever want to do what it is you want to do. But you could create a variable and create and if statement saying.

if (you are localhost)
    $p = "localhost"
else 
    $p = "www.othersite.com"

<a href='http://'.$p.'>Click me</a>

that isn't going to be correct from a syntax standpoint, but it's the basic pseudocode

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文