2

I want to have requests for the www subdomain or for alternate top-level domains redirected to one canonical URL.

To avoid HTTP/HTTPS issues, I figured the easiest way would be to just send a scheme-relative URI in the Location header, like so:

HTTP/1.1 301 Moved Permanently
Location: //example.com/

This seems to work fine in browsers, but the toy »validator« at http://no-www.org/ does not handle it correctly. Is this just a single badly written script, or is this behavior actually more common in scripts, crawlers, etc. out there?

dnadlinger
  • 5,883
  • 4
  • 21
  • 24
  • [Relative URIs are now permitted in redirects](http://stackoverflow.com/a/25643550/545127), so you can expect this to work in all future browsers. – Raedwald Sep 03 '14 at 11:58

1 Answers1

3

Location expects an absolute URI:

[…] The field value consists of a single absolute URI.

Location       = "Location" ":" absoluteURI

Although most user agents will also accept relative URIs, you should stick to the specification and provide an absolute URI.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Oh, thanks, I totally missed that »absolute« part – I just assumed that any URL should work since all UAs I tested behaved as intended… – dnadlinger Dec 08 '10 at 12:24
  • @klickverbot: The rule of thumb is: *Be tolerant with the input but be strict with the output.* – Gumbo Dec 08 '10 at 12:25
  • 7
    It's a bug in RFC 2616 that is fixed in the next revision: http://trac.tools.ietf.org/wg/httpbis/trac/ticket/185 – Julian Reschke Dec 08 '10 at 12:55
  • @Julian Reschke: Oh, really? But it won’t harm to use the stricter variant. – Gumbo Dec 08 '10 at 12:59
  • It's a bug in RFC 2616 but this RFC is still in force and it requires Location to have an absolute URL. There is a **[proposal](http://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-26#section-7.1.2)** to change that but it is still awaiting approval. – rustyx Feb 11 '14 at 17:03
  • rustyx - that proposal actually has been approved (in February) – Julian Reschke Jun 02 '14 at 13:42