urlencode() the 'asterisk' (star?) character urlencode() the 'asterisk' (star?) character php php

urlencode() the 'asterisk' (star?) character


It is okay to have a * in a URL, (but it is also okay to have it in its encoded form).

RFC1738: Uniform Resource Locators (URL) states the following:

Reserved:

[...]

Usually a URL has the same interpretation when an octet is represented by a character and when it encoded. However, this is not true for reserved characters: encoding a character reserved for a particular scheme may change the semantics of a URL.

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

On the other hand, characters that are not required to be encoded (including alphanumerics) may be encoded within the scheme-specific part of a URL, as long as they are not being used for a reserved purpose.


Wikipedia suggests that * is a reserved character when it comes to URIs, and that it must be encoded if not used for the reserved purpose. According to RFC3986, pages 12-13:

URIs include components and subcomponents that are delimited bycharacters in the "reserved" set. These characters are called"reserved" because they may (or may not) be defined as delimiters bythe generic syntax, by each scheme-specific syntax, or by theimplementation-specific syntax of a URI's dereferencing algorithm.If data for a URI component would conflict with a reservedcharacter's purpose as a delimiter, then the conflicting data must bepercent-encoded before the URI is formed.

  reserved    = gen-delims / sub-delims  gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"  sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"              / "*" / "+" / "," / ";" / "="

(The fact that the URL RFC still allows the * character to go unencoded is that is doesn't have a reserved purpose i URLs, and as such doesn't have to be encoded. So wether you have to encode it or not depends on what sort of URI you're creating.)


Javadoc of URLEncoder refers to the HTML specification:

This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.

HTML4 is quite unclear regarding this question and refers to RFC1738, which is quoted by aioobe:

Control names and values are escaped. Space characters are replaced by '+', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., '%0D%0A').

However, HTML5 directly states that * should not be encoded:

  • If the character isn't in the range U+0020, U+002A, U+002D, U+002E, U+0030 to U+0039, U+0041 to U+005A, U+005F, U+0061 to U+007A
    Replace the character with a string formed as follows:
    ...
  • Otherwise
    Leave the character as is.