• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to constract absolute URL from relative URL string

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
how can i get absolute URL from relative URL string like "../foo.html"
like like "http://www.mysite.com/stuff/foo.html"

thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need the base URL as well, like "http://www.mysite.com/stuff/subfolder/". Then just use one of the URL constructors that takes a URL object as the first parameter:

Note that the base URL needs a trailing / if it is a folder, or the "subfolder" part will be considered as a file, and "stuff" will be considered as the current folder.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but what if i have only the site host url like
"www.mysite.com"
and then i have in the links only the relative URl like :
..\..\foo.html
can i somehow construct full uri ?
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code should work

[ September 06, 2008: Message edited by: Rene Larsen ]
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't know the current URL, you can't of course determine the absolute URL of any relative URL. For instance, ../foo.html will have a different absolute URL if found on www.mysite.com (www.mysite.com/foo.html), www.mysite.com/mypath (www.mysite.com/foo.html) or www.mysite.com/mypath/mysubpath (www.mysite.com/mypath/foo.html).

So your first step is: determine the full current URL, or you can't do a single thing. If you don't know the domain, you can actually add any domain and later cut it off again. "localhost" tends to be a good one
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic