0

I am using the Wikipedia API to get information and display it in my web application.

I use a URL like the one below to access the API with an AJAX request in my JavaScript. http://en.wikipedia.org/w/api.php?format=json&action=query&prop=revisions&titles=Barack%20Obama&rvprop=content&rvsection=0&rvparse

However, in the returned content, hyperlinks are specified with relative URLs of the form '/wiki/resource'. Now, when I display this content on my web page, the URLs become like

http://localhost:8003/wiki/resource

How can I make the URLs redirect to http://en.wikipedia/wiki/resource instead?

Thanks in advance!

  • 1
    Just convert it to an absolute URL. Append `http://en.wikipedia` before the relative URL. – mason Apr 22 '14 at 12:58
  • I've managed to fix the problem using a string replace solution. I'm replacing all occurances of '/wiki' with 'http://en.wikipedia.com/wiki'. Any thoughts on how good a fix this is? – Krishna Chaitanya Apr 22 '14 at 13:06
  • 1
    Yes. That's a terrible fix. What if somewhere later in the URL the text "/wiki"? Your link will be incorrect. Please try what I said, just put the base URL in front of it and concatenate the relative URL onto it. – mason Apr 22 '14 at 13:13
  • 1
    I have created [a bug to add option for absolute URLs to the API](https://bugzilla.wikimedia.org/show_bug.cgi?id=40128) some time ago, but it didn't receive much attention. – svick Apr 22 '14 at 14:49

1 Answers1

0

Concatenate the base URL in front of it. For example:

wikiRelativeUrl = getRelativeUrl(); //returns something like /wiki/resource
baseUrl = "http://en.wikipedia";
fullUrl = baseUrl + wikiRelativeUrl;
//use fullUrl
mason
  • 31,774
  • 10
  • 77
  • 121