52

I am unclear if an anchor tag should come before a query string or after.

http://www.domain.com/search?query=hello#name

or

http://www.domain.com/search#name?query=hello

Or does it matter?

  • 1
    Test it. Only one of them works, so it does matter. When I tried anchor first then it thinks the ? is part of the "#" fragment and thus doesn't hop to the right anchor (since there isn't one that has ? in its name...) and probably wouldn't include any info if I checked for the query string in PHP. The last one I haven't tested. – Julix Sep 21 '17 at 17:10
63

Best practice is to append the named anchor at the end. The technical name is a fragment identifier, and the syntax is in RFC 3986.

The RFC section is here: http://tools.ietf.org/html/rfc3986#section-4.1

relative-ref = relative-part [ "?" query ] [ "#" fragment ]

Wikipedia actually covers it well:

The fragment identifier introduced by a hash mark # is the optional last part of a URL for a document

|improve this answer|||||
  • Cheers @iivel! Section 3 was more helpful for me (section-4.1 seems specific to relative URIs) as I debated this with someone. – eebbesen May 19 '15 at 19:03
  • What about webserver, does it need to handle it during serving page ? – Behrouz.M May 17 '16 at 12:12
  • 1
    @raypixar URL fragments are not sent to the server – Juan Mendes Jul 7 '16 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.