0

Some URL's on my website are linked to my XAMPP local host IP (37.15.xxx.xxx) so I have to change the IP to 'localhost' every time to access/test the URL's.

(Example: '37.15.xxx.xxx/file.php' to 'localhost/file1.php')

Is there any way to avoid changing it? I've already tried from 'hosts' (37.15.xxx.xxx localhost) but it does not work.

Pau Guerra
  • 39
  • 7
  • Please be more specific, are you talking about your server code ? your HTML ? can you post a minimal, reproductible problem ? – madjaoue Jul 14 '18 at 17:09

2 Answers2

2

You can simply use relative URLs which means you can remove server address, check this

Or you can make a global variable SERVER_ADDRESS and refactor the code so you could change one line for that or put it in another file which is different on server and local.

TheMMSH
  • 476
  • 5
  • 21
0

localhost is always 127.0.0.1, you are not supposed to change that. What you need is to create a static address to your development machine so that your server always have access to it even if your IP changes. There are a few ways of doing that.

You can use a "Free dynamic DNS service" provider (such as dynu.net) to create a DNS name that is always updated with your IP address, your server can access that DNS whenever it needs to reach your development machine. Easy to use, not recommended though for numerous reasons (DNS may take a while to update if your IP changes, your local setup will also be accessible directly from any peer in the Internet, maybe your IP gets cached and takes even longer -- or never -- to update so the solution might not even work at all).

Personally I'd use a VPN. You can setup a VPN between your dev machine and the server, so that you are always accessible with the same IP within the VPN. If your remote IP changes, it doesn't matter because your VPN IP is static. If you have to use a backup connection that doesn't even support port forwarding such as your neighbor's Wi-Fi, it doesn't matter either, with a VPN tunnel everything just works as long as you have a working Internet access.

Personally I use OpenVPN, recently I heard tinc is better and easier to use but I'm not familiar with this alternative.

Havenard
  • 25,140
  • 4
  • 32
  • 60
  • I have registered a free DNS and I have added it to the hosts file. It's working perfectly. Thank you both for your alternatives. – Pau Guerra Jul 14 '18 at 19:22
  • @PauGuerra You don't have to add it to the hosts file, that is the whole point of registering a DNS. – Havenard Jul 14 '18 at 23:10
  • DNS redirects to the public IP. I can't access from my home. This is the reason of using DNS added to hosts file. (127.0.0.1 > DNS name)... because '127.0.0.1 > Public IP' does not work... – Pau Guerra Jul 15 '18 at 17:57