Allow automatic group membership based on subdomain

Continuing the discussion from Automatically add users to group based on part of email domain:


At the minute the group membership setting “Users who register with an email domain that exactly matches one in this list will be automatically added to this group” – where the admin wants to match – only matches second-level domains like

I propose that this be changed also to match subdomains, e.g.


  1. It would not be feasible for me manually to type in the subdomains for two government-related domains in particular: each has hundreds of subdomains, which are constantly changing, and the full list is not publicly available.

  2. It would provide consistency with email_domains_whitelist, as noted by @sam in Automatic registration domain wild card - #3 by sam

    Yeah there is an inconsistency internally, our automatic group membership is based off a strict domain match per:

    discourse/automatic_group_membership.rb at 40546af21746d469e39dfdcc9ca48987fef6cf3c · discourse/discourse · GitHub

    Our matcher for the email_domains_whitelist uses this match

    discourse/email_validator.rb at 40546af21746d469e39dfdcc9ca48987fef6cf3c · discourse/discourse · GitHub

    I think it makes sense to make this coherent and consistent.

    @techAPJ can you adjust the group regex… carefully and make sure it is tested so it also allows subdomains.

Proposed solutions

This is possible by a minor amendment to pattern = "@(#{domains.gsub('.', '\.')})$" at discourse/discourse/blob/20de49c8722f8e50e93732702a8d06570376edcd/app/models/group.rb#L1017

  1. Changing this to pattern = "@.*(#{domains.gsub('.', '\.')})$" worked for me when I tested it on my forum.

  2. I imagine that pattern = "@(.+\\.)?(#{domains.gsub('.', '\.')})$" (based on email_domains_whitelist) would also work but I only noticed that later and haven’t tested it. I don’t understand why it’s not (.+\.)? but maybe the \\ is a Ruby thing.

An alternative (if you were worried about users who currently intend to exclude subdomains from automatic membership) would be to allow wildcards, e.g. to type * for automatic group membership.


Thanks for considering this request.


Just for ease of reference, and perhaps to make this feature request seem more popular than its mere four likes, here are two other times that this feature has been requested. The first was four years ago, and the second just last month.