0

I have a somewhat annoying problem in website development/deployment process; when I work locally on my server, the address of my program is localhost:8080/MYSERV/..., whereas when I deploy it, the address is www.mysite.com/...

I want all the links in my webpages to be hardcoded to the deployment version. I have read some hosts-file tutorials, but I'm still lost as to how to locally redirect requests to www.mysite.com/ to my local server for development. I would very much appreciate if someone could point me to the obvious one-liner. Thanks a lot in advance!

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
vivri
  • 203
  • 2
  • 10
  • Welcome to [sf]. Please do not tag questions with "SOLVED" or place answers in the question. Instead, post your own answer as an Answer below, and mark them solved by clicking the outline of the tick mark so that it turns green. – Michael Hampton Aug 02 '14 at 21:06

2 Answers2

3

Internal links shouldn't be using absolute URLs, because then they need to be reconfigured when the site is deployed under a different hostname (as you've discovered), and make life difficult when reverse proxying is introduced. Links should preferably be relative to the current location (so that the site can easily be deployed under an arbitrary top level path), or if that's not feasible, relative to the root path (i.e. /images/blah). The only real reason to use an absolute URL is when linking between HTTP and HTTPS (and this is probably easier to do in the web server configuration).

mgorven
  • 30,615
  • 7
  • 79
  • 122
  • Hey, thanks for the quick response! What happens when I use relative links e.g. "serv/main" is that at first the page is loaded fine, and the link (to itself, to keep things simple) shows "localroot/serv/main". When I click it and the page reloads, the link shows "localroot/serv/serv/main" (other links and css are also jumbled-up, as it points to the nonexistent "localroot/serv/serv/style.css"). I'm not really sure as to the cause of the issue.. – vivri May 24 '12 at 04:40
  • It is possible to get that right (the leading and trailing slashes are significant), but the easiest is to use `/localroot/serv/main`. – mgorven May 24 '12 at 04:46
  • That's what I'm doing now - inserting the damn thing dynamically and having to tweak it each time I deploy - but this is putting unnecessary load on the server and is outright ridiculous.. The interesting thing is that on the deployment environment, relative links work perfectly, and I can't figure out why this won't work locally. The only difference in the address is that locally I'm using "localhost:8080/MYSERV/" whereas in deployment it's directly pointing to "www.mysite.com/". Could you please elaborate WRT the slashes - maybe I just missed some permutation in my trial-and-error... Thanks! – vivri May 24 '12 at 05:05
2

Your hosts file generally overrides DNS entries, but its uses are generally for problems other than the one you've described. Using absolute paths in your code isn't a good idea, so I suggest you start using relative paths instead.

To answer your question, the host file contains 2 entries per line, first the IP address followed by the hostname. So for example:

161.114.221.123  www.mysite.com

If you request www.mysite.com from your browser, there will be no DNS lookup and instead your browser will request www.mysite.com directly from 161.114.221.123. So in your case, the hosts file entry would look like this:

127.0.0.1  www.mysite.com

The only problem I can see with this setup is that when you attempt to request www.mysite.com from your browser, it will contact the remote server on port 80, where as from the example you've given it looks like your localhost webserver is actually listening on port 8080. You'll need to reconfigure your local webserver to listen on port 80, and also set up a corresponding vhosts file so that it responds to requests for www.mysite.com.

Richard Keller
  • 2,040
  • 2
  • 19
  • 31
  • Richard, thanks for your answer! Please see the discussion in mgorven's answer; I did attempt to use a relative path, with little success. If that fails, I'll be compelled to reconfigure both my httpd and tomcat, s.t. tomcat will listen on 80 - something I hoped will not be necessary. – vivri May 24 '12 at 12:32
  • Great. Please add your solution as an answer, and mark it as the accepted answer instead of marking the question as 'solved'. – Richard Keller May 28 '12 at 21:49