Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:41:54+00:00 2026-06-17T18:41:54+00:00

Came across something odd today, which perhaps has a simple explanation. If I create

  • 0

Came across something odd today, which perhaps has a simple explanation.

If I create an NSURLRequest using an NSURL that was originally constructed relative to a base URL that is a subdirectory, NSURLRequest removes the subdirectory.

This is much easier to explain with code:

// create a base URL to work with
NSURL *baseUrl = [NSURL URLWithString:@"http://www.google.com/sub"];

// create a URL relative to the base URL
NSURL *url1 = [NSURL URLWithString:@"/foo/bar" relativeToURL:baseUrl];

// create a NSURLRequest using this first URL
NSURLRequest *mangled_req = [[NSURLRequest alloc] initWithURL:url1];
NSLog(@"url1: %@", url1);
NSLog(@"mangled_req: %@", mangled_req);

// now create another URL that puts the base URL together with the relative path
NSURL *url2 = [NSURL URLWithString:@"http://www.google.com/sub/foo/bar"];

// and create a NSURLRequest, which should be the same as above
NSURLRequest *correct_req = [[NSURLRequest alloc] initWithURL:url2];
NSLog(@"url2: %@", url2);
NSLog(@"correct_req: %@", correct_req);

The output demonstrates the point:

2013-01-25 11:55:37.386 url1: /foo/bar -- http://www.google.com/sub
2013-01-25 11:55:37.408 mangled_req: <NSURLRequest http://www.google.com/foo/bar>
2013-01-25 11:55:37.409 url2: http://www.google.com/sub/foo/bar
2013-01-25 11:55:37.409 correct_req: <NSURLRequest http://www.google.com/sub/foo/bar>

Note that the “mangled_req” omits /sub.

This is annoying me right now, because I’m using AFNetworking and want to switch between localhost for testing (which naturally has my web application in a subdirectory) and the remote server (which doesn’t).

Certainly there are workarounds, but this seems odd enough to me that I must be doing something wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:41:55+00:00Added an answer on June 17, 2026 at 6:41 pm

    NSURL is behaving correctly and your assumptions about how URLs work are wrong. A “relative” URL that starts with a slash, as /foo/bar does, always means relative to the host, not relative to the existing path. So if I add /foo/bar as a relative URL to any url scheme://host/path/1/2/3/whatever I will always get back scheme://host/foo/bar. A prefix / on the path means the path is absolute.

    Of course, if you fix this such that your relative URL is foo/bar, you’ll find that you still have a problem, because your original URL did not have a trailing slash. Just as how if I click a link that points to somefile.html while visiting http://host.com/foo/index.html I end up at http://host.com/foo/somefile.html instead of http://host.com/foo/index.html/somefile.html, NSURL will remove the last path component if it does not have a trailing slash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Flash CS5 I came across something odd today. The UIScrollBar component has the
So I'm just learning C#, and came across something that I find odd... I'm
Looking at the source of a website today I came across something that surprised
When reviewing code I came across something odd, someone had read that you can
Came across something like this today, and was wondering if there was an equivalent
I came across something very basic but extremely bewildering today. I needed to convert
I've came across something that drives me crazy. please help me :( I'm try
I came across something weird today, and I was wondering if any of you
I came across something interesting that I can't seem to find any more information
I came across something peculiar today. Take a look at this code snippet: List

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.