During my PHP code I have got references to some URL on another website. I am testing this site in your area (using XAMPP). Can you really get Apache to instantly replace the domain of the other website for localhost?

For instance my PHP code may be:

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

And I would like for my local Apache to alias this to

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

Is possible?

[edit] Simply to clarify, I do without altering PHP code. Is probably not possible, but thought it worth asking because it will make testing in your area simpler without needing to have hacks within the PHP code.

Cheers, Serta.

Apache supports aliases for virtual hosts. Make use of the ServerAlias directive and merely create an alias for that other domain.

You will need to improve your hosts file (what OS are you currently using?) to suggest another domain to localhost (therefore it does not perform a regular old DNS research). Restart Apache and you ought to maintain business.

It does not seem just like a project for Apache not really in mod_rewrite, because the sequence of occasions is the following:

  1. Your PHP script produces HTML for that page while using code you provided, with regards to www.othersite.com
  2. Apache ONLY transmits this code towards the user's browser.
  3. Once the user clicks the hyperlink, the BROWSER attempts to access www.othersite.com. Your Apache is not related to this task.

The thing you need here's something to publish-process the output produced because of your PHP script. You know what: PHP can already do this. You can search for an Apache module that does processing on output before delivering it towards the client, but it is redundant if you are using PHP.

The answer suggested by Silmaril89 looks pretty seem in my experience possess a variable that informs whether you are on "testing mode" and make your Web addresses based on that.

I am unsure the reason why you would ever wish to accomplish what it's for you to do. However, you could produce a variable and make and when statement saying.

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

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

that is not likely to be correct from the syntax perspective, but it is the fundamental pseudocode