tags:

views:

252

answers:

3

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'&gt;Click me</a>"; ?>

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

<?php echo "<a href='http://localhost'&gt;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.

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.'&gt;Click me</a>

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

Silmaril89
Sorry, I probably didn't explain it very well (will update original post to clarify what I mean). I don't want to change the PHP code. It would just make testing locally easier if I could leave the original URL as is.
Dan
A: 

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.

Roadmaster
"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" <--- This sounds pretty much what I'm after. I don't want to change the PHP source code (see my edit in the initial post). Sorry if I was unclear.
Dan
+1  A: 

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.

jasonbar
Great, that worked! :-) I'm using WinXP. The host file was in "Windows\System32\drivers\etc". Thanks for that :-)
Dan