Solved

Relative vs. absolute links in web pages

Posted on 2014-02-20
4
258 Views
Last Modified: 2014-02-25
I am working with a web site designer for a client and have run into an issue of how internal links in a web page should be constructed.  I have always subscribed to the idea that all internal (same base URL) links on a site should be relative (no //www.mysite.com at the beginning) for a whole variety of reasons.  An obvious one is that it makes the site much easier to move to another location (e.g. put it in //www.mysite.com/development or on a local computer for a new version of the site while testing and evaluating).

As a "reality check" I stated viewing the code of existing sites and found that most (of my very small survey) use absolute addressing.  This surprised me.

Is there an advantage to the absolute addressing that I am missing?

I'm wondering if these sites were generated by code rather than by a human directly in which case they can be regenerated with the new base URL if they need to be moved.

Any good insight on this would be appreciated.
0
Comment
Question by:CompProbSolv
4 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
Are we talking about this
//www.mysite.com

or this
/mypage.html

The first one is to accommodate moving between http and https easily without any security warnings being issued because you are serving and http linked resource on https whilst still maintaining a full domain url.
The second one is easier to maintain and still gives you relative links regardless of the protocol

Removing //www.mysite.com from all links saves you a few bytes in bandwidth and is not really needed at all.

What you are seeing in other sites maybe a server side built url
You would still need the full domain and protocol if you wanted links on SSL pages to link to the non SSL page.   This is usually done with server side code to determine if to add the full url.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 125 total points
Comment Utility
I think you need to make your own rules on this.  Even though I almost never move a site, I usually use 'relative' links.  One exception to that is when you have to go from insecure 'http' to secure 'https' because you can't use 'https' without the full link that needs to follow it.

I think you're right about some of the sites being generated by code.  Wordpress sites seem to almost always have absolute links.
0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 125 total points
Comment Utility
Do whatever is the easiest. If you're manually creating all your links, use relative, otherwise you're hard-coding in a URL (be careful of how you use an opening /)

If your pages are generated dynamically, then you'll either be forced into a decision by a particular framework, or you'll have the option. Setting a base_url in one place may be all that's needed to change all links.

Personally I prefer relative when hand coding (easy to move from server to server, and short to type), and I tend to go with whatever when creating a site that's dynamically built - some systems create relative and others create absolute!
0
 
LVL 20

Author Comment

by:CompProbSolv
Comment Utility
Thanks to all for the responses.  This is very helpful.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now