57

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
  • 2
    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, 2017 at 17:10

1 Answer 1

72

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: https://www.rfc-editor.org/rfc/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

3
  • 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, 2015 at 19:03
  • What about webserver, does it need to handle it during serving page ?
    – Behrouz.M
    May 17, 2016 at 12:12
  • 1
    @raypixar URL fragments are not sent to the server Jul 7, 2016 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.