views:

2086

answers:

3

Hello,

I know how to set up a local webserver using xampp on windows... I enter my alias and target on the hosts file (c:\windows\system32\drivers\etc\hosts) and then add a respective entry on my apache vhosts config file. This way, assuming that my webserver is listening to port 80, I can for example map example.com to my local webserver.

I've always entered the whole domain name (that is e.g. example.com) in my hosts file and any requests on that name would be directed to localhost.

Now I was wondering if there's a way to only forward example.com on a certain port (for example only example.com:8080) to the local webserver, and leave example.com (on the default port 80) alone, so that it would still go to my live production website.

As far as I understand this might not be possible using only the hosts file (I tried adding the port :8080 to my domain names - didn't seem work ;-) )...

I really don't know much on this topic so any ideas, insights, links, reading material, tools are welcome.

Edit: Arnout's reply answers the question I've asked above but doesn't solve my actual problem. Rerouting example.com:8080 to localhost:80 does work and if I access example.com it loads up the frontpage of my local version, but all links on that page of course don't know about the port number and therefore point to the production version... The actual solution to my problem seems to be to bite into the sour apple and fix my application (following Rob's suggestion) and remove all hardcoded urls, so that it works on any domain...

+1  A: 

You are playing in the area of domain name services (DNS). Technically, with an advanced DNS configuration (which can include remapping ports), what you propose is possible and it is done routinely on the Internet. However, it is unlikely that you would want to go to that much trouble and expense locally.

On the other hand, I suspect that your real issue can be addressed more easily. Why would you want to have "example.com" resolve to your local web server? You can already reference your local web server as "localhost", as "127.0.0.1", and via its assigned machine name "workstation-x".

The only reason that I can think of, and that I have seen, for wanting to reference your local web server with the same host name as your production web server is due to hard-coding the server host name into your links within your application's web pages. If that is the case here, the answer is simple: don't! You should ALWAYS implement your web applications so that they reference everything (other pages, CSS, JS, images, etc.) relative to the deployed server. If you deploy across multiple servers, then your references must be absolute based on configurable server host names. This is easy to do, and there is no reason not to do it.

If this is not the case, then please explain what you are trying to achieve and what motivates you to try.

EDIT: Since you have confirmed my guess that the problem is hard-coded references to the production host name in the web pages, let me simply add that I have ALWAYS, WITHOUT EXCEPTION, found that it is cheaper to fix the real problem than to accumulate workarounds (such as your attempt to remap ports). I have never encountered an author who has argued the opposite.

Fixing the pages should be little harder than a global search & replace, especially with a decent text editor. Even writing a simple script to change the source files would be vastly cheaper and easier than your attempts at a workaround.

Regardless, best wishes to you, and let us know how it turned out.

Rob Williams
I don't think this is DNS-related. DNS doesn't care about ports, just about hostnames and IP addresses.
Arnout
Well yes, unfortunately it is hard coded and I can't do much about it. I know that the way you're describing it is how it should be done but sometimes, especially when working with older code, you might not have a choice; and simply using a different port seems "cheaper" than a software rewrite...
Ben
+2  A: 
Arnout
This works and answers my initial question... ;-)
Ben
A: 

Can you confirm if 8080 port is listening on webserver? use the command "netstat -na" to see if the port 8080 is listening.

Did you add 8080 port on apache config file as the same as the default 80 port?

Once you add port 8080 on apache config file to make it listen and double check if 8080 port is listening on webserver, then you will be able to access the page using port 8080. like http://mytestwebserver:8080/index.html or something like this....