Skip to content

Instantly share code, notes, and snippets.

@aaronpk
Last active July 2, 2025 12:16
Show Gist options
  • Save aaronpk/5846789 to your computer and use it in GitHub Desktop.
Save aaronpk/5846789 to your computer and use it in GitHub Desktop.
Added WebFinger support to my email address using one rewrite rule and one static file.
[[email protected] www]$ cat .htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} resource=acct:(.+)
RewriteRule ^\.well-known/webfinger /profile/%1? [L]
[[email protected] www]$ cat profile/[email protected]
{
"subject": "acct:[email protected]",
"links": [
{
"rel": "http://webfinger.net/rel/avatar",
"href": "http://aaronparecki.com/images/aaronpk.png"
},
{
"rel": "http://webfinger.net/rel/profile-page",
"href": "http://aaronparecki.com/"
},
{
"rel": "me",
"href": "http://aaronparecki.com/"
}
]
}
@steelman
Copy link

steelman commented Nov 19, 2024

Alas according to the documentation RewriteMap cannot be declared in a per-directory context including .htaccess. So one probably need to name their json files with %40 instead of @ or symlink them because some clients (e.g. Mastodon) send unescaped requests. This also requires to support both : and %3a

RewriteCond %{QUERY_STRING} resource=acct(:|%3[Aa])([^&]+)
RewriteRule ^\.well-known/webfinger /webfinger/%2? [NE,T=application/jrd+json;charset=UTF-8]

@roseeng
Copy link

roseeng commented May 26, 2025

A lot of good info here, unfortunately not in a copy-pastable format (steelman's comment put the profiles in a different subfolder and fmarier accidentally let the dot stay unescaped). Also, with my current provider, it is better to put folder-specific directives in that folder instead of a directive.

So here is my attempt at combining all good ideas from above:

Create a folder called profile
In it, create a .htaccess file with the following contents:

DefaultType application/json
Header set Access-Control-Allow-Origin: "*"

Also create a file for your profile (named [email protected]).
Fill it with the suitable json (no point in me giving you an example).

Then, in your root folder, add the following at the beginning of your .htaccess file:

RewriteEngine on
RewriteCond %{QUERY_STRING} resource=acct(:|%3[Aa])([^&]+)
RewriteRule ^\.well-known/webfinger /profile/%2? [NE,T=application/jrd+json;charset=UTF-8]

If your base folder needs it (i.e. you try it but get a 404) , change the rewrite rule to

RewriteRule ^/\.well-known/webfinger /profile/%2? [NE,T=application/jrd+json;charset=UTF-8]

And last, you try it by navigating to https://your-domain.com/.well-known/webfinger?resource=acct:[email protected]
To double-check that url-encoded calls will work, also try fetching https://your-domain.com/.well-known/webfinger?resource=acct%3Ayour-name%40your-domain.com

@fmarier
Copy link

fmarier commented Jun 26, 2025

@roseeng
Copy link

roseeng commented Jul 2, 2025

@roseeng According to the spec, the correct test URLs should use resource= instead of profile=:

* https://fmarier.org/.well-known/webfinger?resource=acct:[email protected]

* https://fmarier.org/.well-known/webfinger?resource=acct%3Afrancois%40fmarier.org

Right, I mixed up the url parameter and the folder name. Fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment