Apache/PHP - 别名传出 URL
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
这听起来不像是 Apache 的工作;即使在 mod_rewrite 中也不行,因为事件顺序如下:
这里您需要的是对 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:
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.
我不确定为什么你会想做你想做的事。但是您可以创建一个变量并创建 if 语句。
从语法的角度来看,这是不正确的,但它是基本的伪代码
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.
that isn't going to be correct from a syntax standpoint, but it's the basic pseudocode