0

Instead of having the client resolve URLS, I want to let my webserver do that. So ideally, I could turn this:

<a href="http://www.long-domain-name.com/">

into this:

<a href="http://domain/">

By putting an entry in my /etc/hosts file, or DNS magic, or something similar. I'm running nginx. Any way I can accomplish that?

Thanks!

Lin
  • 2,909
  • 7
  • 27
  • 25

5 Answers5

3

You seem to be confusing "qualification" with "resolution".

Resolution is converting the name to an IP address. The client absolutely needs to do this. The only way to avoid this would be to create URLs with the IP address, like so: http://127.0.0.2/ but that's generally a bad idea and, in many cases, will not work.

Qualification is expanding http://example/ to http://www.example.com/. This also technically needs to be done by the client. However, if all you're trying to avoid is typing the long name, you could use server side includes to expand something shorter into the full URL. Ugly hack, but doable.

Probably the most logical workaround, if you absolutely can't stand the thought of typing the long URLs, would be to write a Makefile that runs your source HTML through sed to convert the short versions to long versions.

Insyte
  • 9,394
  • 3
  • 28
  • 45
2

Like others said, you cannot alter DNS resolution. It comes way before your webserver got involved at all. If you just want to have shorter URL for the same domain, you can use Apache and mod_rewrite.

e.g., i redirect automatically all mY "example.com" to "www.example.com". You can easily accept all requests for "mydomain.com" and expand that into "www.my-domain-which-has-a-very-long-name.com".

Obviously, you have to register and set DNS records both for "mydomain.com" and "my-domain-which-has-a-very-long-name.com".

Francesco Abeni
  • 575
  • 1
  • 4
  • 14
1

The client still has to be able to resolve the DNS no matter what.

The only way around it is if your code or something else physically grabs the page itself and then sends it to the client as its own.

Will
  • 826
  • 2
  • 9
  • 19
1

This sounds like something that is normally done by a reverse proxy.

Consider this setup:

The canonical solution is Apache's mod_proxy, but I believe nginx can do this also.

crb
  • 7,998
  • 1
  • 38
  • 53
0

if that www.long-domain-name.com URL is in a page being served from www.long-domain-name.com, then you don't even need to specify the domain. all browsers will fetch an unqualified link from the same site that it came from.

i.e. use a site-relative URL rather than absolute and replace:

<a href="http://www.long-domain-name.com/">

with:

<a href="/">

doing this is a good idea anyway, as it makes it easy to move the site to a different domain name. highly recommended, almost essential, if you want to have a three-stage deployment like "dev.long-domain-name.com" -> "testing.long-domain-name.com" -> "www.long-domain-name.com"

a good rule of thumb in any web development is to avoid absolute URLs whenever possible.

cas
  • 6,783
  • 32
  • 35