A naive PHP+Apache implementation of the redirections and proxying necessary to obtain a beautiful vanity URL for Mastodon. So even if you host your instance in a subdomain, you can still refer your friends to your top-level domain instead.
E.g. your Mastodon server points to mastodon.example.com
but you don't want your friends to have to follow @user@mastodon.example.com
- you want them to follow @user@example.com
Based on information from...
- this post that does sort of the same, but uses Django instead: https://aeracode.org/2022/11/01/fediverse-custom-domains/ - thank you, Andrew!
- this post which uses the above and explains things neatly: https://simonwillison.net/2022/Nov/5/mastodon/ - thank you, Simon!
- this post https://masto.host/mastodon-usernames-different-from-the-domain-used-for-installation/ - thank you, Hugo!
Assuming that:
- you control the domain:
my-super-cool-domain.tld
- your mastodon server is in a subdomain:
masto.my-super-cool-domain.tld
- Get a copy of this project.
- Place the files in the root folder of your top level domain, i.e. where you want your Mastodon URLs to appear to be. But watch out if you already have an
.htaccess
, you'll need to manually merge them (see Limitations section below). With our example, you place the files in the root ofmy-super-cool-domain.tld
. - Edit
.htaccess
and to replace{YOUR_DOMAIN}
with the name of your mastodon server (for example:masto.my-super-cool-domain.tld
). - Rename
config-dist.php
toconfig.php
and customise it: change the value ofredirect_to_server
to the URL of the Mastodon server you're using.
Now requests to ${your_domain}/@your_user
and other requests to well-known
files should be automatically redirected and/or or proxied appropriately.
Assuming that $redirect-to-server is the value of your mastodon server and you followed the config steps above...
- proxies
.well-known/webfinger
to$redirect-to-server/.well-known/webfinger
- redirects
.well-known/host-meta
to$redirect-to-server/.well-known/host-meta
- proxies
.well-known/nodeinfo
to$redirect-to-server/.well-known/nodeinfo
- redirects
@user
to$redirect-to-server/@user
I've written this in a few hours and it's the first time I do this sort of well-known
manipulation so if something breaks I would not be surprised. Proceed with caution.
My current server uses Apache so if you need nginx rules, you'll have to write them yourself! Have a look at .htaccess for what you need to do. Hopefully it's simple enough that it's not too hard. There are also some pointers in the post from Masto.host that explain how to do this in Nginx.
Also if your server already has content that conflicts (e.g. .htaccess or paths starting with an @
like the ones for the users), then this might clash to some degree.
Hopefully it is helpful. Contributions are welcome but please note this isn't intended to be a fully fledged project, more like a snippet of code for you to use or get started.
Good luck and happy federating! 🤩
I had issues following users from other servers. I think I have fixed it by not using the naive proxying I was doing before, and just using Apache redirections, as of this commit a19b885. I think that might close issue #1 as the issue sounded similar, but I find debugging this a bit tedious and I have not time to do it now.
I have a sort of a hunch that maybe it would be easier to just use more .htaccess rules for the rest of paths, but I haven't explored this yet. Just leaving it here as a note for the future when I have some free time again (or if/when things start breaking).
If you know of a way to debug these things without much suffering or if you know the answer to my hunch, feel free to leave a comment or open an issue. I might pick it up a few months later, but I will be grateful all the same!
Thank you.