[go: up one dir, main page]

Mastodon - be findable with your domain (on Firebase hosting)

While it’s cool to host your own Mastodon server, it’s not really efficient. A simple thing you can do, however, is to make it easier for others to discover your presence, if they already know your website. Try it out – search for “johnmu@johnmu.com” in your Mastodon instance. Also, click “follow” for SEO tips & other bad takes.

Why is hosting your own instance inefficient? In short, sending updates between users (followers, followees) means connections between their individual servers, and if everyone has their own server, it’s a lot of network activity. It costs you money, it costs them money. It’s better if you share instances (and support your server).

Be discoverable with your domain

The solution is Webfinger. On the basis of a set of “well-known” URLs that you host on your website, a social network like Mastodon can look up your account, and present that to the user. We don’t have to make the contents of these URLs ourselves (though you could), we just need to redirect to our official ones.

Practically speaking, you have to redirect two URLs (and tweak their query parameters):

  • /.well-known/host-meta
  • /.well-known/webfinger

The query parameters (as stolen from another blog post) is the same for each one: ?resource=acct:username@domainname. To make this work with Mastodon in general, you will need to swap out the username and domainname to match the account that you actually use.

For me, that looks like this:

  • 301 redirect:
  • From: https://johnmu.com/.well-known/webfinger?resource=acct:john@johnmu.com
  • To: https://mastodon.social/.well-known/webfinger?resource=acct:johnmu@mastodon.social

If you try the link, you should get an XML file from mastodon.social, showing that my account exists. Maybe it still exists when you read this post?

Redirects on Firebase hosting

I ran into a slight snag when doing this for the Firebase static hosting that I use. In particular, you can’t redirect with query parameters. What year is this? Anyway, luckily that doesn’t stop me.

Workaround: URL shorteners will redirect for you. I use bit.ly. I set up short URLs for each of the URLs needed, and used Firebase redirects to point to them. My firebase.json includes the following:

{
  "hosting": {
    // ...
    "redirects": [
    {
      "source": "/.well-known/host-meta",
      "destination": "https://bit.ly/3UmafyI", // "https://mastodon.social/.well-known/host-meta?resource=acct:johnmu@mastodon.social"
      "type": 301
    },
    {
      "source": "/.well-known/webfinger",
      "destination": "https://bit.ly/3UH7DuU", // "https://mastodon.social/.well-known/webfinger?resource=acct:johnmu@mastodon.social"
      "type": 301
    }
    // ...
    ]
}

This works surprisingly well. As a by-product of not supporting URL querystring parameters, any username@johnmu.com address will point to my Mastodon account. I don’t know if this is good or bad, but feel free to add me with “johnmu@johnmu.com”. Also, I don’t have email set up, so spam away.

The social plus

Back in the days of Google+, it was en-vogue to set up a /+ URL on your site to point to your Google+ profile, since those URLs were really weird. Let’s do that here too, for old-times sake. In my firebase.json I include:

{
    // ...
    "redirects": [ 
    {
      "source": "/+",
      "destination": "https://mastodon.social/@johnmu",
      "type": 302
    },

Yes, https://johnmu.com/+ is a 302 redirect. Who knows where it’ll point to next?

And just for sake of having too many redirects, I also set up https://johnmu.com/@johnmu to do the same redirect. It’s probably overkill, just pick one profile redirect URL and stick to it is easier. Right?

WordPress

Shortly after posting this, Michael Nolen mentioned how you can do this directly in WordPress, using the Redirection plugin. Also, he suggested that it’s probably enough to only redirect the webfinger .well-known URL.

WordPress redirect for webfinger

(screenshot from Michael Nolen)

Apache & co

I have no practice in Apache redirects anymore. I’m guessing something like this will do it in your .htaccess file:

RewriteEngine On
RewriteRule ^.well-known/host-meta(.*)$ https://mastodon.social/.well-known/host-meta?resource=acct:johnmu@mastodon.social [L,R=301]
RewriteRule ^.well-known/webfinger(.*)$ https://mastodon.social/.well-known/webfinger?resource=acct:johnmu@mastodon.social [L,R=301]

Let me know if I got this wrong, or rather, if there’s a better way to do it in Apache. If you have a post explaining this for other servers, happy to link there too.

Other platforms

I found these, let me know if you have others to link to:

Misc

Social proof

Screenshot of Mastodon showing a search for johnmu@johnmu.com

Usage

With the URL redirector, you can check the usage to see how often it’s actually used over time. Is it worth it? After 3 months, not a ton of usage, but it does seem to work:

Screenshot of bitly stats

  • /.well-known/nodeinfo: 6 uses
  • /.well-known/host-meta: 9 uses
  • /.well-known/webfinger: 64 uses (which seems like a sign that this is the one you really need)

Updates

  • 2023-02-11 - Removed /.well-known/nodeinfo mention – it’s apparently only needed for Mastodon instances. Thanks, Braedon!
  • 2023-02-11 - Added bitly stats for redirects
  • 2022-11-09 - Added a link to the WordPress version, and Cloudflare pages, Django, nginx links.

Comments / questions

There's currently no commenting functionality here. If you'd like to comment, please use Mastodon and mention me ( @hi@johnmu.com ) there. Thanks!

Related pages