Proper URL forming with a query string and an anchor hashtag

When both a query string and anchor tag (hash tag) are visible in a URL, what is the proper order for them to appear?

http://www.whatever.com?var=val#anchor

or

http://www.whatever.com#anchor?var=val

Is there any documentation on this?

The URLs are being handled by WordPress / PHP.


Solution 1:

?var=var#hash

Everything after # is client side.

Also, look into URL rewriting to get rid of ugly ?var=var.

Solution 2:

? should come before the # as noted in RFC 3986:

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

Taken from an answer over at Super User (Does an anchor tag come before the query string or after?):

Solution 3:

Note that when the URL has both anchor tags (#) and query strings (?), the browser may ignore the query string and navigate to the anchor tag without reloading the page.

It may be necessary to submit the page using a

<form action='webpage.php?q=string#tag' method='GET or POST'>
    <input type='text' id='q' name='q' value='string'>
    <input type='submit' value='submit'>
</form>

rather than just a URL link

<a href='webpage.php?q=string#tag'>.