开发者

Apache/PHP - alias outgoing URL

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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜