Question

I use an installation of Xampp to test my files locally before I upload them to my web server. My web pages usually contain a lot of 'hardcoded' links, which means to test any links (or any forms etc) I need to change the hardcoded link to point to localhost, and then rechange it after I am done testing.

What I am looking for is a way to redirect an external url to point to my localhost folder, so I could set it as my website and then I would be able to make changes before I upload them. For example, if I were to enter 'www.mysite.com/example/view.php" in my browser, I would actually be looking at 'localhost/example/view.php'.

Is such a thing possible?

Was it helpful?

Solution

Two solutions come to my mind:

  1. Use relative URLs (preferred)
  2. Add your external URL to your hosts file which can be found either in

    C:\WINDOWS\system32\drivers\etc\hosts (Windows)

    or

    /etc/hosts (Linux)

    Just add this line:

    www.mysite.com localhost

    Maybe you have to use your servers ip instead of mysite.com, I'm not sure right now

OTHER TIPS

I just stumbled upon this.

Use this line in hosts:

127.0.0.1 www.yoururl.com

Yes : dont use hardcoded links !

Seriously, I'm not sure if I understood correctly. If you are talking of URLs in your pages pointing to other pages on the same site, you should use links of the form "/example/view.php" instead of "http://www.mysite.com/example/view.php". That way, you will always point to the same site.

If those links go to external sites, but you need to redirect them locally for testing (I cant see why this would be the case, but you never know ...) then you could use a varable in a config file. The you would just need to change this variable at one place ...

If your remote website has a static IP, you can use a redirect rule on your internet router. Turn the rule on and off as required.

Trap traffic going to 202.20.20.20 (The website IP) and redirect to 192.168.1.10 (Your local PC IP)

This method redirects after the DNS resolution rather than before. This, and the other method above (using the Hosts file) is really useful if using software such as wordpress; which hardcodes the development URL in it's database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top