How to Redirect All Email from One Domain to Another in Postfix While Preserving Usernames


2 views

When managing email servers, it's common to encounter situations where you need to redirect emails from a misspelled domain to the correct one while preserving the original usernames. For example, all emails sent to user@tedswigets.com should be automatically redirected to user@tedswidgets.com.

Postfix's virtual_alias_maps feature is perfect for this scenario. Here's how to implement it:

# In /etc/postfix/main.cf
virtual_alias_domains = tedswigets.com
virtual_alias_maps = hash:/etc/postfix/virtual

Then create/edit your virtual aliases file:

# /etc/postfix/virtual
@tedswigets.com @tedswidgets.com

After making these changes, remember to:

postmap /etc/postfix/virtual
postfix reload

This configuration will redirect all emails while preserving the username portion. For example:

  • john@tedswigets.comjohn@tedswidgets.com
  • sales@tedswigets.comsales@tedswidgets.com

If you're managing multiple domains and only need this redirection for specific pairs, simply add them to your virtual file:

# /etc/postfix/virtual
@misspelled1.com @correct1.com
@misspelled2.com @correct2.com

Always test your setup after implementation:

postmap -q "@tedswigets.com" hash:/etc/postfix/virtual

This should return @tedswidgets.com if configured correctly.

For high-volume email servers, consider using postmap with dbm or cdb instead of hash for better performance with large alias files.


In my Postfix mail server setup, I handle multiple virtual alias domains. Among these, I have two similar-looking domains where one is a common misspelling of the other (e.g., tedswidgets.com vs tedswigets.com). I need to automatically redirect all incoming emails for any address @tedswigets.com to its corresponding address @tedswidgets.com while preserving the local part (username) of the email address.

For this solution to work properly, you should already be using Postfix's virtual alias domains configuration. This is typically set up in these files:

/etc/postfix/main.cf:
    virtual_alias_domains = hash:/etc/postfix/virtual_domains
    virtual_alias_maps = hash:/etc/postfix/virtual_aliases

Postfix's virtual alias maps support pattern matching using regular expressions. We can leverage this to create a catch-all redirect that preserves usernames:

/etc/postfix/virtual_aliases:
    /^(.+)@tedswigets\.com$/ $1@tedswidgets.com

After making changes, don't forget to rebuild the hash database:

sudo postmap /etc/postfix/virtual_aliases
sudo systemctl reload postfix

If you have multiple domain pairs that need similar treatment, you can extend the pattern:

/etc/postfix/virtual_aliases:
    # Primary domain correction
    /^(.+)@tedswigets\.com$/ $1@tedswidgets.com
    
    # Another domain pair example
    /^(.+)@acmeinc\.com$/ $1@acme-inc.com

Always test your redirects after implementation. You can use Postfix's built-in checking:

postmap -q "user@tedswigets.com" hash:/etc/postfix/virtual_aliases

This should return "user@tedswidgets.com". For more thorough testing, actually send test emails through your server.

While regex patterns in virtual aliases are powerful, they do add some processing overhead. If you're handling high volumes of email, consider these optimizations:

  • Place more frequently matched patterns higher in the file
  • Keep your patterns as simple as possible
  • Monitor your mail logs for any performance issues

For more complex scenarios where you need to change both domain and username, you might consider using transport maps:

/etc/postfix/transport:
    tedswigets.com   tedswidgets.com

But this approach would require additional configuration and is generally overkill for simple domain corrections.