Domain aliasing with Node.js/Express
Heads up: This is a technical process that requires access to your server configuration. Test in a staging environment before deploying to production.
You can use a domain from your Express web server for branded short links by setting it up as an alias domain in Rebrandly and installing the rebrandly-express package on your server to handle routing.
While this approach works, we recommend using a dedicated domain for your branded links whenever possible. A dedicated domain requires no server configuration, has fewer points of failure, and removes the dependency on your website staying correctly configured. See How do I get a branded domain? if you'd like to take that path instead.
Compatibility note: The
rebrandly-expresspackage (v0.0.6) has not been updated in several years. It is confirmed working on Express 4. Compatibility with Express 5 (released September 2024) is unconfirmed — test against your Express version in a staging environment before deploying.
Prerequisites
Before you begin, make sure you have:
- A Rebrandly account
- A domain you own
- An Express 4 web server with Node.js installed
- Access to modify your server code
Step 1: Add the domain to your Rebrandly account
- Log in to Rebrandly.
- Click Domains.
- Click New Domain.
- Click Connect a domain you already own.
- Enter the domain name.
- Select No, it's being used for my website.
- Click Use domain directly.
- Click Save.
- Copy the alias domain. Keep this page open—you'll need it for your server configuration.
You can also find the alias domain later from your Domains list by clicking Server Configuration.
Step 2: Install and configure the Rebrandly Express package
Install the rebrandly-express npm package:
npm install [email protected]GitHub: https://github.com/rebrandly/rebrandly-express
Add the package to your Express application. Replace your-alias-domain.com with the alias domain you copied in step 9.
const rebrandlyRouter = require("rebrandly-express");
const options = { "alias": "your-alias-domain.com" }; // Replace with your alias domain
const fallbackToRebrandly = rebrandlyRouter(options);
// Initialize your app as usual and add your own middlewares...
// Add Rebrandly right before your catchall rule for 404
app.use(fallbackToRebrandly);
app.use(your404Handler);Restart your Express server.
For detailed configuration options, see the rebrandly-express documentation on GitHub.
Step 3: Test the integration
Create a branded link in Rebrandly using your alias domain, then open the short link in your browser. If everything is configured correctly, it should redirect to your destination URL.
How it works
The rebrandly-express middleware intercepts requests that would normally reach your 404 handler and checks whether a Rebrandly branded link exists for that path. If a matching link is found, the user is redirected to its destination. If no match is found, the request continues to your own 404 handler.
Links created in Rebrandly with an alias domain only activate when the path is not already handled by your website. Your existing routes always take priority.
