Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've recently been working on a project and I've been seeing two different forms of url's being used.

src="../MyLinkUrl"

and

src="/MyLinkUrl"

We are using MVC .NET and the times I have seen the .. usages have been in Javascript code files and not when it's been generated by the MVC URL helpers. Typically the usages range from specifying a controller action to specifying images.

Both seem to work but I'm wondering which is the better practice or if it depends on the context. Some questions I have are:

  1. What benefits/pitfalls does one have over the other
  2. Is one better practice than the other
  3. Does it depend on context and if so what context?
share|improve this question
3  
Are you asking generally about the difference between relative and absolute paths? –  Steven Burnap Feb 11 '13 at 20:29
    
ummm, are these not both relative paths? –  dreza Feb 11 '13 at 20:56
3  
im no file path wizard but doesn't ../MyLink mean that my link is in the parent directory of the working directory, while /MyLink is in the working directory. –  Ryathal Feb 11 '13 at 21:04
1  
@Ryathal o I thought /MyLink was relative to the root directory??? I'm not actually sure how that works then when the current directory is something like /NewDir/MeDir and I want to have the url reference /OtherDir/Me.jpg...? –  dreza Feb 11 '13 at 21:34
    
@Ryathal - dreza is correct, /MyLink is an absolute path. You're thinking of MyLink, which is a relative path in the current directory/level (and is the same as ./MyLink) –  Izkata Feb 12 '13 at 1:18

2 Answers 2

up vote 8 down vote accepted

When you see /path/to/file that is an absolute path. Starting from the server's root, it's followed down to the file. With just /file that means that you're looking directly in the server's root.

Whenever you do not have a leading slash, the path is taken as relative. This means that, starting from your current point in the file system, you follow the path. ../file would mean to look in the parent directory for file. If you had just file it would look in the current directory.

share|improve this answer
    
Thanks. Relative to Windows Web URL when you mean servers root do you mean the root of the location where the solution is on the server, not the root drive of the server itself? Also any comments on which one in general to use when specifying urls? –  dreza Feb 11 '13 at 21:43
3  
Relative to whatever the server is configured to serve. Modern servers can get funny with / and /foo being in one place and /bar being on a completely different place. It's best to think of URLs referring to "resources". As for which to use, I generally stick to absolute paths unless I have a good reason to do otherwise. –  Sean McSomething Feb 11 '13 at 22:07
    
One question. Is /path/to/file not a relative path in that it's relative to the root? –  dreza Feb 16 '13 at 19:21
    
@dreza It's 'absolute' in the sense that it refers to a single, unambiguous file/directory, regardless of your current working directory. Relative paths are always relative to the CWD. –  Sean McSomething Feb 19 '13 at 17:04

It is probably better to use relative path (../../otherdir/file.html) as it changes less frequently and you may save on maintenance and refactoring. For instance, correcting some spelling mistake in the folder name will always break the absolute path to that folder but may not break the relative path.

share|improve this answer
    
What happens when you change the URL to be more SEO friendly from /MyApp/MyUrl/ to something like /MyApp/MyProduct/MyLocation/MyUrl. Would that not then break all the links that have ../ notation where as a relative root path (/otherdir/...) would be maintained?? –  dreza Feb 12 '13 at 19:10
    
You can move all relevant pages level down or level up as long as all are moved together. You will probably move the whole site to more SEO friendly location so this would work. –  h22 Feb 12 '13 at 21:08

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.