Can I use asterisks in URLs?

21,463

Solution 1

Using an asterisk (*) in a URL is not such a great idea. Because it's a reserved character, it's not used anywhere else; even though your URL scheme seems user-friendly to you, few will think to try it, and it's likely to give unpredictable results because the meaning of wildcards in URLs is hard to discern. (I couldn't tell what all of your examples meant until I read your descriptions.)

Not only that, but there might be some more semantic/meaningful ways to do what you describe. For example, you could append a query string and use a 'find' and 'where' variable to tell your method what to find where:

Find pages starting with 'search phrase' in /some/folder/:

example.com/some/folder/?find=search-phrase&where=start

Find pages with 'search phrase' anywhere:

example.com/some/?find=search-phrase&where=anywhere

To show all pages, I would use a separate method called 'all' instead of a query string or wildcard syntax:

example.com/some/folder/all

The query string syntax is far more common than asterisks -- look in your address bar next time you do a Google search, for example -- and it will likely be easier to code too.

Finally, if you don't like the look of query strings, you could prepend a method name called 'search' and then use the next two blocks as the 'find' and 'where' variables. e.g. Instead of:

example.com/some/folder/?find=search-phrase&where=start

You could have:

example.com/some/folder/search/search-phrase/start

Then, you just need to check for the 'search' keyword in your URL path, and trigger the your search method using the next two path segments as variables.


UPDATE: I found an asterisk in a URL today. The new archive.org interface is using it exactly as you describe (as part of a search feature), in place of an 'all' keyword. e.g.:

http://wayback.archive.org/web/*/http://google.com

instead of

http://web.archive.org/web/20040214050058/http://www.google.com/

The first example returns archived listings from all dates for google.com, rather than just pages from a certain date (second example). Interestingly, I can't link to the live page here, because the Stack Exchange site encodes the * character as %2a when it appears in URLs, which results in a 404 from archive.org. (Perhaps another reason not to use asterisks in URLs.)

I still think it's not quite as clear as 'all', but, if you're looking for examples of other sites adopting asterisks in their URLs, that's the first one I've seen.

Solution 2

Yes, because it's a reserved character.

Other reserved characters

The asterisk ("*", ASCII 2A hex) and exclamation mark ("!" , ASCII 21 hex) are reserved for use as having special significance within specific schemes.

From here: https://datatracker.ietf.org/doc/html/rfc3986#section-2

EDIT:

Section 2, on characters, has been rewritten to explain what characters are reserved, when they are reserved, and why they are reserved, even when they are not used as delimiters by the generic syntax. The mark characters that are typically unsafe to decode, including the exclamation mark ("!"), asterisk ("*"), single-quote ("'"), and open and close parentheses ("(" and ")"), have been moved to the reserved set in order to clarify the distinction between reserved and unreserved and, hopefully, to answer the most common question of scheme designers.

From here: http://labs.apache.org/webarch/uri/rfc/rfc3986.html#modifications

Share:
21,463

Related videos on Youtube

KajMagnus
Author by

KajMagnus

I develop https://www.talkyard.io — it's like open source StackOverflow, but with open-ended discussons, chat, and embedded comments. Unless otherwise mentioned: All original source snippets that I post on StackOverflow and StackExchange sites are licensed under Creative Common's CC0 (in addition to the license prescribed by StackOverflow.) Contact: kajmagnus3 at gmail ...

Updated on September 18, 2022

Comments

  • KajMagnus
    KajMagnus over 1 year

    Are there any reasons I shouldn't use an asterisk (*) in a URL?

    Background:

    With asterisks, I could provide these nice and user friendly (or what do you think??) URLs:

    • example.com/some/folder/search-phrase*
      means search for pages with names starting with "search-phrase", located in /some/folder/.

    • example.com/some/**/*search-phrase*
      means search for any page with "search-phrase" anywhere in its name.

    • example.com/some/folder/*
      means list all pages in /some/folder/ (rather than showing the /some/folder/index page).

    • Admin
      Admin almost 13 years
    • Admin
      Admin almost 13 years
      Here's another similar SO question: Can a URL have an asterisk?. I didn't find them, because I spelled Asterisk with Asterix :-/ until I was corrected (see the Asterix reply below :-)).
    • Admin
      Admin almost 13 years
      @KajMagnus I found a site for the first time today using asterisks in their URLs. See my updated answer below.
  • KajMagnus
    KajMagnus almost 13 years
    Hmm, * is a reserved character, yes. But it's a sub-delimiter, and when I read the URI spec it seems to me that it's fine to use sub-delims in an application specific manner, in the path component of the URI. -- Doesn't this mean it's OK to use * (and also e.g. @, :, +, ,) in the way I did?
  • KajMagnus
    KajMagnus almost 13 years
    I'm confused by the phrase "are reserved for use as having special significance within specific schemes". Can I not define my own application specific scheme? Could that be intention, with sub-delims, that web apps use them in a web app specific manner?
  • KajMagnus
    KajMagnus almost 13 years
    W.r.t. Asterix: What! Did you think I meant "Asterisk"?
  • KajMagnus
    KajMagnus almost 13 years
    W.r.t. Asterix: Oh that's why I didn't find any relevant pages on StackOverflow or Webmasters. Now I found 2 related pages on S.O.: Allowing asterisk in URL and Can a URL have an asterisk? -- thanks :-)
  • KajMagnus
    KajMagnus almost 13 years
    For what it's worth, here someone says Wikipedia allows asterisks in URLs and someone else says it's okay: Can a URL have an asterisk?
  • KajMagnus
    KajMagnus almost 13 years
    (In case anyone wonders about the reference to Asterix: I misspelled Asterisk, I thought it was Asterix :-))
  • Alex
    Alex almost 13 years
    You asked if you should use it, not if it's technically possible or not, and the general idea is that unless you know exactly what you are doing, you should not use it.
  • KajMagnus
    KajMagnus almost 13 years
    I don't know if I agree with that general answer. A way to learn new stuff, is to do something that you don't know how it works, and then you'll learn and find out. How would our future be if people in general did only things that they knew exactly how they were working? (It's a pre-alpha-version that I'd be releasing, so it's not meant for production :-)) -- Thanks for the info and links anyway :-)
  • Alex
    Alex almost 13 years
    I totally agree with you about the learning new stuff. However, the discussion about URL design is a little more complicated that this. Here is a good article about the use of "special" characters in URLs (in this case the hashbangs): danwebb.net/2011/5/28/it-is-about-the-hashbangs
  • KajMagnus
    KajMagnus almost 13 years
    Hashbangs were new to me, an interesting read :-) There was a link to another interesting article too: Cool URLs never change.
  • KajMagnus
    KajMagnus almost 13 years
    When I read the URI spec, my interpretation is that in the HTTP scheme it's fine for a webapp to use !*+ etc as it sees fit. The HTTP scheme isn't very strict (in comparison to the other schemes listed), as far as I can tell from Wikipedia. Well perhaps I should open another question about the HTTP scheme and the URI spec. Anyway, I'll probably have to accept the other answer, which argues from an end user's point of view, since I don't understand whether or not this URI spec answer is correct.