Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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".

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
If you really need to, then there's <base href="http://www.long-domain-name.com/path/" />. –  koenigdmj Jul 21 '09 at 23:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.