LoginSignup
26
18

More than 5 years have passed since last update.

Mediumの『/@{username}』みたいにURLの中で『@』を使っているのは大丈夫なのか調べてみた

Last updated at Posted at 2016-03-11

MediumのURLでユーザー名の前に『@(アットマーク)』が入っているのを見かけます。

https://medium.com/@narikei

/users/narikeiとかなっているよりもタイプ数も少ないし、直感的で良い思った
でも、他にやっているところを見ないし『@』をURLに含んでいるサービスもない

これはインターネットの仕様的に大丈夫なのか疑問だったので調べてみました。

URLおさらい

例えば、こんなURLがあった場合

https://username:password@www.example.com:8080/news/index.html?page=2&flg=true#popular

このような要素に分類される

scheme https
host www.example.com
port 8080
user username
pass password
path /news/index.html
query page=2&flg=true
fragment popular

普通URLで『@』っていうとBasic認証で使うイメージです。

『@』を使っても良いか

URLの親玉URIの仕様はInternet Engineering Task Forceが策定している

その仕様書がこれ
http://www.ietf.org/rfc/rfc3986.txt
(読みにくいのでダウンロードして固定ピッチのフォントで読むのがおすすめ)

3.3.  Path

   The path component contains data, usually organized in hierarchical
   form, that, along with data in the non-hierarchical query component
   (Section 3.4), serves to identify a resource within the scope of the
   URI's scheme and naming authority (if any).  The path is terminated
   by the first question mark ("?") or number sign ("#") character, or
   by the end of the URI.

   =====

      segment       = *pchar
      segment-nz    = 1*pchar
      segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
                    ; non-zero-length segment without any colon ":"

      pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"

私のてきとう翻訳によると、Pathには使っても大丈夫って書いてあった
さらに、「:(コロン)」も使ってもよいらしい

ちゃんとパーセントでエスケープする場合は%40を使う

そういえば

むかし、Hashbangってものもあったけど/#!は使っちゃだめ
というか#以降がfragmentとして分類される。

おわり

MediumはきちんとURIの仕様にそっていて正しかった
/@narikeiだと既存のルーティングと競合しないし、/@loginとかやられても辛さが軽減されるのでオススメかも

Railsだったら こんな感じでルーティングさせることができる

routes.rb
  get "/@:username" => "users#show"

JavaでSpringならこんな感じ

UserController.java
@Controller
@RequestMapping(value = "/@{userName}")
public class UserController {...}

ただ、すべてのブラウザやクローラーがきちんと読んでくれるかは微妙な気がします。

はじめてIETFの仕様書とか読んだ

26
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
18