<p>Are square brackets in URLs allowed?</p>

<p>I noticed that <a href="http://hc.apache.org/httpclient-3.x/index.html" rel="noreferrer">Apache commons HttpClient</a> (3.0.1) throws an IOException, wget and Firefox however accept square brackets.</p>

<p>URL example:</p>

<pre><code>http://example.com/path/to/file[3].html
</code></pre>

<p>My HTTP client encounters such URLs but I'm not sure whether to patch the code or to throw an exception (as it actually should be).</p>

[{'answer_id': 40571, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 1, 'selected': False, 'text': '<p>Best to URL encode those, as they are clearly not supported in all web servers.  Sometimes, even when there is a standard, not everyone follows it.</p>\n'}, {'answer_id': 40583, 'author': '1729', 'author_id': 4319, 'author_profile': 'https://Stackoverflow.com/users/4319', 'pm_score': 3, 'selected': False, 'text': '<p>Pretty much the only characters not allowed in pathnames are # and ? as they signify the end of the path.</p>\n\n<p>The uri rfc will have the definative answer:</p>\n\n<p><a href="http://www.ietf.org/rfc/rfc1738.txt" rel="noreferrer">http://www.ietf.org/rfc/rfc1738.txt</a></p>\n\n<blockquote>\n  <p>Unsafe:</p>\n  \n  <p>Characters can be unsafe for a number of reasons.  The space\n    character is unsafe because significant spaces may disappear and\n    insignificant spaces may be introduced when URLs are transcribed or\n    typeset or subjected to the treatment of word-processing programs.\n    The characters "&lt;" and ">" are unsafe because they are used as the\n    delimiters around URLs in free text; the quote mark (""") is used to\n    delimit URLs in some systems.  The character "#" is unsafe and should\n    always be encoded because it is used in World Wide Web and in other\n    systems to delimit a URL from a fragment/anchor identifier that might\n    follow it.  The character "%" is unsafe because it is used for\n    encodings of other characters.  Other characters are unsafe because\n    gateways and other transport agents are known to sometimes modify\n    such characters. These characters are "{", "}", "|", "\\", "^", "~",\n    "[", "]", and "`".</p>\n  \n  <p>All unsafe characters must always be encoded within a URL. For\n    example, the character "#" must be encoded within URLs even in\n    systems that do not normally deal with fragment or anchor\n    identifiers, so that if the URL is copied into another system that\n    does use them, it will not be necessary to change the URL encoding.</p>\n</blockquote>\n\n<p>The answer is that they <em>should</em> be hex encoded, but knowing postel\'s law, most things will accept them verbatim.</p>\n'}, {'answer_id': 40589, 'author': '17 of 26', 'author_id': 2284, 'author_profile': 'https://Stackoverflow.com/users/2284', 'pm_score': 1, 'selected': False, 'text': '<p>According to the <a href="http://www.w3.org/Addressing/URL/url-spec.txt" rel="nofollow noreferrer">URL specification</a>, the square brackets are not valid URL characters.</p>\n\n<p>Here\'s the relevant snippets:</p>\n\n<blockquote>\n  <p>The "national" and "punctuation" characters do not appear in any\n     productions and therefore may not appear in URLs.<br>\n  national                { | } | vline | [ | ] | \\ | ^ | ~<br>\n  punctuation             &lt; | > </p>\n</blockquote>\n'}, {'answer_id': 40595, 'author': 'Lee', 'author_id': 3556, 'author_profile': 'https://Stackoverflow.com/users/3556', 'pm_score': 3, 'selected': False, 'text': '<p>Any browser or web-enabled software that accepts URLs and is not throwing an exception when special characters are introduced is almost guaranteed to be encoding the special characters behind the scenes. Curly brackets, square brackets, spaces, etc all have special encoded ways of representing them so as not to produce conflicts. As per the previous answers, the safest way to deal with these is to URL-encode them before handing them off to something that will try to resolve the URL.</p>\n'}, {'answer_id': 66579, 'author': 'rjray', 'author_id': 6421, 'author_profile': 'https://Stackoverflow.com/users/6421', 'pm_score': 2, 'selected': False, 'text': '<p>For using the HttpClient commons class, you want to look into the org.apache.commons.httpclient.util.URIUtil class, specifically the encode() method. Use it to URI-encode the URL before trying to fetch it.</p>\n'}, {'answer_id': 1016737, 'author': 'Justin Cormack', 'author_id': 15495, 'author_profile': 'https://Stackoverflow.com/users/15495', 'pm_score': 7, 'selected': True, 'text': '<p><a href="http://www.ietf.org/rfc/rfc3986.txt" rel="noreferrer">RFC 3986</a> states</p>\n\n<blockquote>\n  <p>A host identified by an Internet\n  Protocol literal address, version 6\n  [RFC3513] or later, is distinguished\n  by enclosing the IP literal within\n  square brackets ("[" and "]").  This\n  is the only place where square bracket\n  characters are allowed in the URI\n  syntax.</p>\n</blockquote>\n\n<p>So you should not be seeing such URI\'s in the wild in theory, as they should arrive encoded.</p>\n'}, {'answer_id': 1718238, 'author': 'MM.', 'author_id': 126603, 'author_profile': 'https://Stackoverflow.com/users/126603', 'pm_score': 4, 'selected': False, 'text': "<p>I know this question is a bit old, but I just wanted to note that PHP uses brackets to pass arrays in a URL.</p>\n\n<pre><code>http://www.example.com/foo.php?bar[]=1&amp;bar[]=2&amp;bar[]=3\n</code></pre>\n\n<p>In this case <code>$_GET['bar']</code> will contain <code>array(1, 2, 3)</code>.</p>\n"}, {'answer_id': 3037298, 'author': 'Casebash', 'author_id': 165495, 'author_profile': 'https://Stackoverflow.com/users/165495', 'pm_score': 2, 'selected': False, 'text': '<p>StackOverflow seems to not encode them:</p>\n\n<p><a href="https://stackoverflow.com/search?q=square+brackets+[url]">https://stackoverflow.com/search?q=square+brackets+[url]</a></p>\n'}, {'answer_id': 38709035, 'author': 'sixtytrees', 'author_id': 6447563, 'author_profile': 'https://Stackoverflow.com/users/6447563', 'pm_score': 1, 'selected': False, 'text': '<p>Square brackets are considered unsafe, but majority of browsers will parse those correctly. Having said that it is better to replace square brackets with some other characters.</p>\n'}, {'answer_id': 44901804, 'author': 'oHo', 'author_id': 938111, 'author_profile': 'https://Stackoverflow.com/users/938111', 'pm_score': 5, 'selected': False, 'text': '<p>Square brackets <code>[</code> and <code>]</code> in URLs are not often supported.</p>\n\n<h3>Replace them by <code>%5B</code> and <code>%5D</code>:</h3>\n\n<ul>\n<li><p>Using a command line, the following example is based on <code>bash</code> and <code>sed</code>:</p>\n\n<pre><code>url=\'http://example.com?day=[0-3][0-9]\'\nencoded_url="$( sed \'s/\\[/%5B/g;s/]/%5D/g\' &lt;&lt;&lt; "$url")"\n</code></pre></li>\n<li><p>Using Java <a href="https://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html#encode-java.lang.String-java.lang.String-" rel="noreferrer"><strong><code>URLEncoder.encode(String s, String enc)</code></strong></a></p></li>\n<li><p>Using PHP <a href="http://php.net/manual/en/function.rawurlencode.php" rel="noreferrer"><strong><code>rawurlencode()</code></strong></a> or <a href="http://php.net/manual/en/function.urlencode.php" rel="noreferrer"><strong><code>urlencode()</code></strong></a></p>\n\n<pre><code>&lt;?php\necho \'&lt;a href="http://example.com/day/\',\n    rawurlencode(\'[0-3][0-9]\'), \'"&gt;\';\n?&gt;\n</code></pre>\n\n<p>output:</p>\n\n<pre><code>&lt;a href="http://example.com/day/%5B0-3%5D%5B0-9%5D"&gt;\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>&lt;?php\n$query_string = \'day=\' . urlencode(\'[0-3][0-9]\') .\n                \'&amp;month=\' . urlencode(\'[0-1][0-9]\');\necho \'&lt;a href="http://example.com?\',\n      htmlentities($query_string), \'"&gt;\';\n?&gt;\n</code></pre></li>\n<li><p>Using your favorite programming language... Please extend this answer by posting a comment or editing directly this answer to add the function you use from your programming language ;-)</p></li>\n</ul>\n\n<p>For more details, see the <a href="https://www.ietf.org/rfc/rfc3986.txt" rel="noreferrer"><strong>RFC 3986</strong></a> specifying the URL syntax. The <strong>Appendix&nbsp;A</strong> is about <code>%-encoding</code> in the query string (brackets as belonging to <em>“gen-delims”</em> to be <code>%-encoded</code>).</p>\n'}]
['https://Stackoverflow.com/questions/40568', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4308/']